File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "static & nonstatic methods" Watch "static & nonstatic methods" New topic

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();
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
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

[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
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.
I agree. Here's the link:
subject: static & nonstatic methods
It's not a secret anymore!