+ 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); } }

1st Oct 2016, 12:22 PM
Moji 99
Moji 99 - avatar
13 Answers
+ 3
First , you shouldn't have two Main(s) then you forgot this line System.out.println(myc.bark(2,3)); :)
1st Oct 2016, 12:35 PM
زياد وجدي
زياد وجدي - avatar
+ 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
1st Oct 2016, 12:44 PM
زياد وجدي
زياد وجدي - avatar
+ 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
1st Oct 2016, 12:50 PM
Moji 99
Moji 99 - avatar
+ 2
u can already , but this is just calling ! when you want to print anything you have to use System.out.print();
1st Oct 2016, 12:54 PM
زياد وجدي
زياد وجدي - avatar
+ 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
3rd Oct 2016, 5:08 AM
K Sravan Kumar
K Sravan Kumar - avatar
+ 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
1st Oct 2016, 2:10 PM
K Sravan Kumar
K Sravan Kumar - avatar
+ 1
Thank you all guys, I got it :)
1st Oct 2016, 2:35 PM
Moji 99
Moji 99 - avatar
+ 1
you should use something to receive the return or direct output(sorry my poor English)
2nd Oct 2016, 4:14 PM
ccc
0
It says: No Output
1st Oct 2016, 12:22 PM
Moji 99
Moji 99 - avatar
0
You mean I should forget about creating new object of that class? I didnt get where exactly shall I put System.out.println().
1st Oct 2016, 12:39 PM
Moji 99
Moji 99 - avatar
0
Hmmm I see, thank you Shravan :)
3rd Oct 2016, 11:53 AM
Moji 99
Moji 99 - avatar
0
just copy and paste the function within main function
5th Oct 2016, 5:38 PM
RAJKAMAL J
RAJKAMAL J - avatar
0
program needs a main() to run
12th Oct 2016, 4:50 PM
Nirmal Chandra Panda
Nirmal Chandra Panda - avatar