在类内部使用静态方法时没有输出(该类内部没有主函数)

huangapple go评论106阅读模式
英文:

No output while using static methods inside a class(the class which has no main function in it)

问题

以下是您提供的内容的翻译部分:

这是我的主类:

  1. public class App {
  2. public static void main(String[] args) {
  3. Student s1 = new Student();
  4. }
  5. }

这是创建的类:

  1. class Student {
  2. public static void f1() {
  3. f2();
  4. }
  5. public static String f2() {
  6. return "hello";
  7. }
  8. public Student() {
  9. f1();
  10. }
  11. }

现在,我在主类中创建了一个名为s1的对象,构造函数被调用,该构造函数包含f1(),所以f1()被调用,现在f1()调用了f2(),所以f2()被调用,因此我认为"hello"必须被打印出来,但实际上没有输出任何内容(什么都没有打印出来)。有人能解释一下可能的原因吗?

英文:

THIS IS MY MAIN CLASS:

  1. public class App {
  2. public static void main(String[] args){
  3. Student s1=new Student();
  4. };
  5. };

THIS IS THE CREATED CLASS:

  1. class Student {
  2. public static void f1(){
  3. f2();
  4. }
  5. public static String f2(){
  6. return "hello";
  7. }
  8. public Student(){
  9. f1();
  10. }
  11. }

Now , as i have created an object s1 in main class, the constructor is called ,which has f1() , so f1() is called , now f1() has f2(), so f2() is called , so i think "hello" must be printed but the output is not printed at all(nothing is printed). Can anyone please explain what the reason could be?

答案1

得分: 4

打印返回一个值之间有一个区别。
如果你希望它被打印出来,你应该尝试像这样做:

  1. class Student {
  2. public static void f1(){
  3. f2();
  4. }
  5. public static void f2(){
  6. System.out.print("hello");
  7. }
  8. public Student(){
  9. f1();
  10. }
  11. }
英文:

There is a difference between printing and returning a value.
If you want it to get printed, you should try doing something like this:

  1. class Student {
  2. public static void f1(){
  3. f2();
  4. }
  5. public static void f2(){
  6. System.out.print("hello");
  7. }
  8. public Student(){
  9. f1();
  10. }
  11. }

答案2

得分: 3

"f2()"正在返回"String",但是"f1"没有将其打印出来:

  1. public static void f1(){
  2. System.out.println(f2());
  3. }
  4. public static String f2(){
  5. return "hello";
  6. }
  7. public Student(){
  8. f1();
  9. }
英文:

f2() is returning the String, but f1 is not printing it:

  1. public static void f1(){
  2. System.out.println(f2());
  3. }
  4. public static String f2(){
  5. return "hello";
  6. }
  7. public Student(){
  8. f1();
  9. }

答案3

得分: 3

要在控制台日志中打印,您应该尝试:System.out.println("Hello");

您正在返回该值,而不是打印它。

英文:

To be printed at the Console Log you should try: System.out.println("Hello");

You are returning the value not printing it.

答案4

得分: 2

你必须使用 System.out.println("Hello"),而不是 return "hello";

英文:

You have to use System.out.println("Hello") instead of return "hello";

答案5

得分: 2

I方法....

由于f2方法具有返回类型,为了获取从该方法获得的值,需要创建一个与返回类型兼容的引用,并使用相同引用代码编写单词"hello",如下所示:

  1. class Student {
  2. public static void f1() {
  3. String x = f2(); // 调用方法
  4. System.out.println(x);
  5. }
  6. public static String f2() {
  7. return "hello";
  8. }
  9. public Student() {
  10. f1();
  11. }
  12. }

II方法......

你可以尝试这种方式:

  1. class Student {
  2. public static void f1() {
  3. System.out.println(f2()); // 调用方法
  4. }
  5. public static String f2() {
  6. return "hello";
  7. }
  8. public Student() {
  9. f1();
  10. }
  11. }
英文:

I method....

Since the f2 method has a return type, in order to get the value obtained from it, make a reference of the type that is compatible with the return type and write the word hello using the same reference code as follows

  1. class Student {
  2. public static void f1(){
  3. String x=f2(); //method calling
  4. System.out.println(x);
  5. }
  6. public static String f2(){
  7. return "hello";
  8. }
  9. public Student(){
  10. f1();
  11. }

}

II method......

You can try this way...

  1. class Student {
  2. public static void f1(){
  3. System.out.println(f2());//calling method
  4. }
  5. public static String f2(){
  6. return "hello";
  7. }
  8. public Student(){
  9. f1();
  10. }}

huangapple
  • 本文由 发表于 2020年8月23日 16:49:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/63545084.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定