• Post Reply Bookmark Topic Watch Topic
  • New Topic

Static method in Java can be accessed using object instance  RSS feed

 
Subhash Pavuskar
Ranch Hand
Posts: 57
Chrome Eclipse IDE 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?
 
John Jai
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
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@John Jai: Thanks for reply.
 
Jeff Verdegan
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?

 
Paul Clapham
Sheriff
Posts: 22835
43
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.



 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate 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: 22835
43
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!