Reputation: 53
public class Zoo
{
public String coolMethod(){
return "wow !! baby..";
}
public static void main(String args[])
{
Zoo z=new Zoo();
z.coolMethod();
}
}
This is a simple code to display a string but it is not showing the desired output i.e. in this case string "wow !! baby.." .It is compiling and executing fine but not showing the result.
Upvotes: 0
Views: 144
Reputation: 920
1st way
public class Zoo
{
public String coolMethod(){
return "wow !! baby..";
}
public static void main(String args[])
{
Zoo z=new Zoo();
System.out.println(z.coolMethod()) ;
}
}
2nd way
public class Zoo
{
public void coolMethod(){
System.out.println( "wow !! baby..");
}
public static void main(String args[])
{
Zoo z=new Zoo();
z.coolMethod();
}
}
In your coolMethod()
method the return type is String
. In order to display the result of this method, you have to return it void
or use System.out.print();
.
For your second problem is, you do not have main method inside your class.Create a new class Test then put this method
public static void main(String[] args) {
Moo m = new Moo();
m.useMyCoolMethod();
}
Upvotes: 0
Reputation: 3833
Whenever you want to produce some output to your console, use System.out.print() method. For example:
System.out.print("fff"); System.out.print("ggggg");
will produce fffggggg. If you want to print the same result in separate line, use System.out.println. For example:
System.out.println("fff"); System.out.println("ggggg");
will produce
fff
ggggg
Upvotes: 0
Reputation: 35557
public class Zoo
{
public String coolMethod(){
return "wow !! baby.."; // return String
}
public static void main(String args[])
{
Zoo z=new Zoo();
z.coolMethod(); //here you are getting "wow !! baby..", but you are not printing it.
}
}
replace
z.coolMethod(); with System.out.println(z.coolMethod()); // now you will see the out put in console
Upvotes: 0
Reputation: 1600
You specified coolMethod() to return, not to print. There won't be any printed output. You also did not assign a string variable to z.coolMethod() nor wrap print around it so your desired output is lost.
Upvotes: 1
Reputation: 17622
The method coolMethod()
just returns the String. But looks like you forgot to put the code to print the string. You may do like this
System.out.println(z.coolMethod());
Upvotes: 8