File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static & nonstatic methods

 
nachiket deshpande
Ranch Hand
Posts: 114
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 114
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Oliver!
 
Prasad Ballari
Ranch Hand
Posts: 149
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic