• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

why static methods can not be overridden  RSS feed

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why static methods can not be overridden ?
 
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the belong to class..not Object
 
Java Cowboy
Sheriff
Posts: 16084
88
Android IntelliJ IDE Java Scala Spring
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static methods cannot be overriden because they belong to the class, not to an instance of that class.
A static method don't need an instance to be accessed.

So, when you think you are overriding a static method, the only thing you are doing is redefining it.

(If I'm wrong just tell me please :P)
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bazilio is correct.
static methods cannot be overridden.

you can define the same static method in different classes and this mechanism is called redefining. The compiler accepts it and doesn't throw any error. But in run time you wont get the expected results of overriding since it is not actually a overriding.
i guess this should clarify your doubt tejahyd
 
Ranch Hand
Posts: 826
Chrome Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ram Prasad wrote:Bazilio is correct.
static methods cannot be overridden.

you can define the same static method in different classes and this mechanism is called redefining.



I think it is known as hiding
 
Guilherme Bazilio
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

sudipo shekhar wrote:
I think it is known as hiding



I think hiding is also a valid name. K&B book calls redefine.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!