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?
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();
}
}
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With