aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes static & nonstatic methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "static & nonstatic methods" Watch "static & nonstatic methods" New topic
Author

static & nonstatic methods

nachiket deshpande
Ranch Hand

Joined: Oct 06, 2000
Posts: 114
i want help regarding the following code,why there is compilation error
class my test{
public static void main{
mytest t=new mytest();
t.print();
}
static void print(){
System.out.println("i am a static method");
}
void print(){
System.out.println("i am a non static method");
}
}
why there is a compilation error regarding print() method!since we have a call to print() in a static method,why cant it call the static print method?
Oliver Grass
Ranch Hand

Joined: Nov 02, 2000
Posts: 65
Hi,
this has to do with the signature of a method. The signature of a method is determined by the name and the number and types of the parameters. The modifiers do NOT belong to the signature. So you tried to define two methods with the same signature. This is not valid and results in a compiler error....

You can call a static method over the class test.print() or over the object t.print(). If you call it over t.print(), how should the compiler know which method you want to call???
Hope that helps
cheers
Oliver

[This message has been edited by Oliver Grass (edited November 13, 2000).]
nachiket deshpande
Ranch Hand

Joined: Oct 06, 2000
Posts: 114
thanks Oliver!
Prasad Ballari
Ranch Hand

Joined: Sep 23, 2000
Posts: 149
Oliver.
I agree with you.One more point i would like add. i.e for method overloading the compiler checks the parameter list & method name as the differentiating points.SO the access modifiers can not be used as main distingushing factor for overloading.
Regards
Prasad
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static & nonstatic methods