• Post Reply Bookmark Topic Watch Topic
  • New Topic

what is the flip side for calling a static method with an object  RSS feed

 
krishnadhar Mellacheruvu
Ranch Hand
Posts: 118
Android Java Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) We can call static method directly in a static/non-static methods
2) We can call a static method with a class name
3) We can as well call a static method with an object(though a warning "The static method Smethod5()(static method5) from the type StaticNonStaticMethods should be accessed in a static way" will be displayed)

Though a warning is displayed for the 3rd statement we can still execute it. What is the flip side  of such successful execution.

Thanks
 
s ravi chandran
Ranch Hand
Posts: 579
6
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unless you are calling a stateless method which doesn't depend on object state to complete its processing, it will be risky to use such a call.

You see, objects are able to access the static method because they are instantiate representation of the class. But when you are calling from various objects and all objects expect different results, that might not work so well.
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a very interesting topic to discuss.
Let we understand it through an example:



please have a look on the 2 reference,initialized and the nullReference,the former as well as the later both will print *...parent *.the  second one succeeds because for invoking a static method jvm needs a class for it which a compiler infer through a declared type and puts it for use during run time.so even if the object is used for the static call still there class of the object is used for the call implicitly.
actually it was considered as a flaw that was not corrected or realized initially so in order to support backward compatibility it was left present as it been.
you must have got this warning on some IDE as they are configured such that they can sense such calls and warn that the way of the call is due to the flaw so please correct it.
some people considered it as a flexibity because suppose after some time you felt to change the static method to instance method.it would still gain you backward compatibility.try it,but null will fail here.
now sometimes people take it in a wrong way that it gains a inheritance(or polymorphism) for static method .lets have an example:


You may be expecting the polyCheck to return "...Child" but it will not.this is all because compiler infers the class through the declared type of reference not the object it is pointing.it is not like that static method cannot be inherited,indeed they are,but you cannot override them as overriding a method is done for the different behaviour of the same method for the subclass instance and in the above subclass example we see that the subclass instance is not showing the different behaviour as expected why?-because static method has nothing to do with instance.
for a reference read about overriding and hiding methods:
https://docs.oracle.com/javase/tutorial/java/IandI/override.html.

Hope it helps!

Kind regards,
Praveen.
 
Junilu Lacar
Sheriff
Posts: 11485
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please cite some credible sources to back up your claim that this is a flaw that was overlooked and left in for backwards compatibility. To be honest, I find the notion to be a bit of a stretch.
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Please cite some credible sources to back up your claim that this is a flaw that was overlooked and left in for backwards compatibility. To be honest, I find the notion to be a bit of a stretch.
Junilu,i really don't have any strong source to claim my words for declaring it as a flaw.but on several forums i noticed some people call it a design flaw,several others took it as a flexibility though.
so a edit for those words-
[EDIT]-*some people like me(as i noticed on several forums) take it as a design flaw...*

here are some link-
https://coderanch.com/t/558628/java/Static-method-Java-accessed-object#2535319
https://www.quora.com/Why-cant-we-call-static-methods-using-the-object-in-Java/answer/Ed-Guenthner
http://stackoverflow.com/a/610674.

Anyway thanks for notifying about it.

Kind Regards,
Praveen.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We know all sorts of weird things were done in the early days, for example permitting the [] before or after the name of an array identifier. The keyword static has changed its meaning since the days of C, so maybe allowing static members be invoked on an instance name is another weird thing. As JuniluPraveen's second link says, it permits bad practice. That shows the dangers of trying to override static methods (which can't be done) or static fields (which can't be done either). The chap on quora is right to say make your IDEs give a warning for inappropriate use of static members (or even change it to an error).

Actually weird things still happen, like the absence of a CharStream suitable for iterating the letters of a String or the unsuitability of Optional<T> for obviating nulls.

[edit]Sorry for misattributing the links.
 
krishnadhar Mellacheruvu
Ranch Hand
Posts: 118
Android Java Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks ravi,praveen,Campbell that was a really insightfull.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!