• Post Reply Bookmark Topic Watch Topic
  • New Topic

Constants  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public static final String NAME="JAVATAR";

"final" makes sure the constant has the same value and prevents it from being changed. So why add "static" to make it a constant. I figured the reason a few weeks back but don't remember it now.. Can somebody explain it in a way I can remember it really well..
 
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please do not use all caps in your posts as it comes off as shouting. Please KeepItDown I have adjusted the title for you this time.

Sometimes, you want to have variables that are common to all objects. This is accomplished with the static modifie

source

static has nothing to do with a variable being a constant. It just defines how you can access it (In this case, since it is static, it would be in the form ClassName.VariableName)
 
Ashwin Raju
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK ..so it's about how you can access it. So lets imagine that we live in a world where "static" did not exist. So you could still use "final" but accessing it would be a little more tedious.correct?
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashwin Raju wrote:OK ..so it's about how you can access it. So lets imagine that we live in a world where "static" did not exist. So you could still use "final" but accessing it would be a little more tedious.correct?

Yes. You would need a getter method to access it.

However, there is much more to static than just how it is accessed. Please go through the link I shared.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:Yes. You would need a getter method to access it...

Erm, actually not. Not if it was public. You'd only need a reference to an instance, viz: x.NAME. Indeed, you can actually use that notation for static constants as well; but I'd advise strongly against it.

Now, whether an instance constant should be public is another matter...

Winston
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Erm, actually not. Not if it was public. You'd only need a reference to an instance, viz: x.NAME. ...
Now, whether an instance constant should be public is another matter...

I stand corrected.
I think I am so used to denying direct access, that making it public did not even cross my mind.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!