• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why we got warning when calling a static method through an instance?

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers;

I have a question.

Why we got warning when calling a static method through an instance of that class?

I mean, the static methods has nothing to do with the object creation of that class, thats why..

Or they are does not contain polymorphic behavior...

Please elaborate.

Thanks.
 
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tahir,

Please note that though it is legal it is not more readable.

Static method is for a class & not for a instance.

But since a instance will always be a type of that class its legal to call the static method
using the instance reference.

Consider this code


This is confusing since no instance is associated with reference t but still this will not
throw any exception & the current executing thread will sleep.

This will become more clear when you read about threads & static methods used.

As a practice it is good to use the Classname.Staticmethod



 
Tahir Akram
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Avishkar for helping the concept.

 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Avishkar Nikale wrote:But since a instance will always be a type of that class its legal to call the static method
using the instance reference.



Not necessarily.
Try this code.
 
Avishkar Nikale
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Joanne,

I should have elaborated that the static method called will depend completely on the reference class type
& not the subsequent class of object the reference is pointing to.

Tahir,

I hope you are not confused by Joanne's example.

It shows that the static method being called will always depend on the class of the reference variable
& not of the underlying instance class.

Please let us know if you need further clarification.


reply
    Bookmark Topic Watch Topic
  • New Topic