Class A{public static void meth1(){System.out.println("Inside method 1");}}public class B{public static void main(String args[]){A a=new A();System.out.println(a.meth1());}}The above code gives a compile error.It says cannot use void.If the return type is not void then it compiles sucessfullywhat could be the reason?

Questions by anandkolam   answers by anandkolam

Showing Answers 1 - 4 of 4 Answers

Sonal Mehta

  • Mar 20th, 2006
 

The reson is , as you are trying to call that method inside SOP.So SOP must have something to print , that is why you need to have some return type for that method.

  Was this answer useful?  Yes

Thanks sonal

i have another doubt as we know all the java classes which we write by default extends the Object class.so when we use main() method in our programs we overide the main() method.i want to know in which class is main() method defined.Also if we overide a method in subclasses the signature and the number of arguments must be the same.but if you overide the main method in your program like this

public static void main(String args[],String args2[])

{

}

it does not give any compile time error.but when you try to run the program it gives an NomethodFound exception

regrds

ANAND

  Was this answer useful?  Yes

Kamesh

  • Mar 30th, 2006
 

Whenever a java program is executed the first thing it checks is for a class with the same name as the program and for a main method with signature looks like this: public static void main(String args[]).  if it is unable to locate this method signature then it will obviously give the very common known error "method not found".  That is the reason why you got the error!!!

  Was this answer useful?  Yes

Sash

  • Jul 12th, 2006
 

and as of Java5 .. you can use variable arguments too.

also the name of the argument can change ... i.e. ...

we can have public static void main(String abc[])

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions