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

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

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

问题

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

这是我的主类:

public class App {
    public static void main(String[] args) {
        Student s1 = new Student();
    }
}

这是创建的类:

class Student {
    public static void f1() {
        f2();
    }
    
    public static String f2() {
        return "hello";
    }
    
    public Student() {
        f1();
    }
}

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

英文:

THIS IS MY MAIN CLASS:

public class App {
	public static void main(String[] args){
		Student s1=new Student();
		
	};
	};

THIS IS THE CREATED CLASS:

class Student {
	public static void f1(){
		f2();
	}
	public static String f2(){
		
		return "hello";
	}
	public Student(){
		f1();
	}

}

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

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

class Student {
    public static void f1(){
        f2();
    }
    public static void f2(){
        
        System.out.print("hello");
    }
    public Student(){
        f1();
    }

}
英文:

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

class Student {
    public static void f1(){
        f2();
    }
    public static void f2(){
        
        System.out.print("hello");
    }
    public Student(){
        f1();
    }

}

答案2

得分: 3

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

public static void f1(){
    System.out.println(f2());
}
public static String f2(){    
    return "hello";
}
public Student(){
    f1();
}
英文:

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

public static void f1(){
    System.out.println(f2());
}
public static String f2(){    
    return "hello";
}
public Student(){
    f1();
}

答案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",如下所示:

class Student {
    public static void f1() {
        String x = f2(); // 调用方法
        System.out.println(x);
    }

    public static String f2() {
        return "hello";
    }

    public Student() {
        f1();
    }
}

II方法......

你可以尝试这种方式:

class Student {
    public static void f1() {
        System.out.println(f2()); // 调用方法
    }

    public static String f2() {
        return "hello";
    }

    public Student() {
        f1();
    }
}
英文:

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

class Student {
public static void f1(){
    String x=f2(); //method calling
    System.out.println(x);

}
public static String f2(){
    
    return "hello";
}
public Student(){
    f1();
}

}

II method......

You can try this way...

class Student {
public static void f1(){
    
    System.out.println(f2());//calling method

}
public static String f2(){
    
    return "hello";
}
public Student(){
    f1();
}}

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:

确定