This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General forum!
  • 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:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Static method in Java can be accessed using object instance

 
Ranch Hand
Posts: 57
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java static methods are created to access it without any object instance. It makes some sense to me. But recently I came across one strange thing that, static method in Java can also be accessed through its object instance. This looks pretty wierd to me. Does anyone of you know why this feature is provided by Java? Whats the significance of allowing static methods getting accessed with as well as without instance?
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my opinion, an object created by a class, should access all the methods present in the class. So it should have the capacity to call the static methods too.

But inside, a static method is never really called on an object instance. Even if the object instance points to null, it gets called.

 
Subhash Pavuskar
Ranch Hand
Posts: 57
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@John Jai: Thanks for reply.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Personally, I think it's a design flaw. It can be convenient to access static members without explicitly naming the class, but it's kind of a hack to do so. For instance, check out the following:



What do you think this class should do?

Now run it. What does it do? Do you understand why it does that?

 
Sheriff
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But consider this code fragment:


Here the instance method "greet" calls the static method "sayHello" without having to look like this:

Instead the code implicitly uses "this", like, um... this:


In this situation it looks a lot less like a hack to allow static methods to be accessed via an object of the class. In fact it would be rather inconvenient to not allow that.



 
Bartender
Posts: 10775
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:In this situation it looks a lot less like a hack to allow static methods to be accessed via an object of the class. In fact it would be rather inconvenient to not allow that.


Hmmm. Seems like extrapolation to me (although you may well be right). Why couldn't the compiler (or runtime) simply substitute the class name rather than this when it encounters an unqualified call to a static method?

I have to admit, I don't have particularly strong feelings about it; but I tend to avoid {instance}.method() calls to static methods myself.

Winston
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:But consider this code fragment:
...
In this situation it looks a lot less like a hack to allow static methods to be accessed via an object of the class. In fact it would be rather inconvenient to not allow that.



Yeah, like I said, it's convenient, and I'm glad we can do it that way in that situation. I still think it's a hack though. :-)
 
Paul Clapham
Sheriff
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:I have to admit, I don't have particularly strong feelings about it; but I tend to avoid {instance}.method() calls to static methods myself.



So do I. Except in this particular situation, I don't tend to avoid calling the static method via the instance; on the contrary, I would avoid calling it via the class name. That's why I pointed it out as a special case.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!