• Post Reply Bookmark Topic Watch Topic
  • New Topic

static and instance method with the same signature  RSS feed

 
Amitav Chowdhury
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java you can't have static method and instance method with the same signature in the same class hierarchy.
Can some one explain me why this is not allowed ?
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When an instance calls this method, how does it know which one to call? Should it always call the instance method unless the class prefix is used?
 
Vedha Vishali
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

You cant have two methods with same signature in a class hierarchy.

Its duplicating methods.(And complier will not allow you to do it . )


Thanks,
VedhaVishali.
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Proper syntax for calling a static method uses the class name. But the same method can be
called on an instance variable also. There can be only one method (static or non-static) that
matches the calling signature.
Since the static designator does not affect a method's parameter list or its return type,
it can not be considered part of its signature.

Jim ... ...
 
Manish Singh
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because that will make code hard to understand if someone try to call a static method using an instance variable
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!