• Post Reply Bookmark Topic Watch Topic
  • New Topic

static members  RSS feed

 
Kaushik Sathupadi
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • 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
  • 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: 56525
172
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!