• 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

static members

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If static members of a class has to be accessed only through the class name, why java supports accessing them through the instance of the class at all ?
 
Kaushik Sathupadi
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is a update to my previous message. Please validate the following:

One of my friends suggested that we should actually prefer to use
reference.staticMethod()

instead of
className.staticMethod()

.. since if in the future we move the static method to some other class then we can just change the reference to point to a new class, instead of chaniging the class name everywhere.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static members seem to be flavour of the month; there is lots of discussion. Here, for example.
I would disagree about using the reference to call a static method (even though it can be done); if you are going to move the static method ( ) you are going to have to update lots of references anyway.

The question is: why should you move a method like that in the first place?
 
Kaushik Sathupadi
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Campbell,
then is there any specific reason why java allows to access static variables through references to the classes? It could have easily been a compiler error!

could it be like this? When the class name is very big, we can create a reference to it with a shorter name and access the static variable through it?
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That question was mentioned in the link I posted yesterday. Has that not sorted out your query?

If you need more information than that, people will be only too happy to try to help.
 
reply
    Bookmark Topic Watch Topic
  • New Topic