-3

I have declared a method in a class named Teacher used return statement,

    public String info(){
    return  "Name is " +name +"Location is "+location;

Then I built the constructor then I called it from main class

Teacher t1= new Teacher("Tim","Guildford","Reader");
t1.info();

However both in cmd windows and intellij , there is no result.

But if I change the method type to void and use system.out.println, every thing is fine.

What is the problem here?

Zishuo Yang
  • 99
  • 1
  • 4

4 Answers4

5

You aren't using the return. You are calling info() but doing nothing with the return. Try this:

Teacher t1= new Teacher("Tim","Guildford","Reader");
System.out.println(t1.info());

Or store the String in a variable and use it later:

String test = t1.info();
brso05
  • 13,142
  • 2
  • 21
  • 40
2

You are not printing the result of your call. Try System.out.println(t1.info());

Lav
  • 1,850
  • 15
  • 17
0

and use system.out.println

Well, you aren't using that in the code you included in your question, that's why nothing ends up in the output.

null
  • 5,207
  • 1
  • 19
  • 35
0

Since info() returns a String, t1.info() is a String. What you have done is to create a String called t1.info() with the value of "Name is " +name +"Location is "+location.

You have to do something with this String now.

If you want to print the String, just print it like this:

System.out.print(t1.info());

RaminS
  • 2,208
  • 4
  • 22
  • 30