+ 1
Why doesn't this code return what I want?
public class Program { int bark(int x, int y){ int z = x+y; return z; } public static void main(String[] args) { } } class Fu{ public static void main(String[] args){ Program myc = new Program(); myc.bark(2,3); } }
13 Answers
+ 3
First , you shouldn't have two Main(s)
then you forgot this line
System.out.println(myc.bark(2,3));
:)
+ 3
public class Program
{
int bark(int x, int y){
int z = x+y;
return z;
}
public static void main(String[] args) {
Program myc = new Program();
System.out.println(myc.bark(2,3));
}
}
Sorry for the short explanation .
First : your Program need one main to run .
second : no need to the class Fu
check the code above , i think u can get it
+ 2
Thank you very much for the explanation, but: Why can't I call the method using the object I had created? myc.bark(2,3); for example
+ 2
u can already , but this is just calling !
when you want to print anything you have to use System.out.print();
+ 2
inheritance missing you can't call,
if you have Main method in with in the class it is considered as main class,
there should be one Main class for each package
you can call methods of super class in the main Class by creating object
+ 1
u can call an instance method by creating object if method is in subclass or with in the class,
if you want to call an instance method that is not with in the main class you must use inheritance
+ 1
Thank you all guys, I got it :)
+ 1
you should use something to receive the return or direct output(sorry my poor English)
0
It says: No Output
0
You mean I should forget about creating new object of that class? I didnt get where exactly shall I put System.out.println().
0
Hmmm I see, thank you Shravan :)
0
just copy and paste the function within main function
0
program needs a main() to run