Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is the definition of......

 
Elahe Shafie
Ranch Hand
Posts: 291
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Friends,
Can you tell me what id the definition of static here?
private final static String _HAPPY = "happy";
Thanks,
ELham
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It means that the String variable named _HAPPY belongs to the class that this statment is in, NOT to the objects of whatever class this is.
In real life that means that none of the objects of this class on the heap have internal fields to store a variable named _HAPPY. But if you look in the classfile that is loaded up before any instances are created, you WILL find a variable with this name. In that variable you will find a reference that points to a location in the String pool that holds the object which is "happy".
Since the _HAPPY field is final, it can never be modified to reference any other String, so it is a constant.
Since the _HAPPY field is private, it can only be accessed by the rest of the code in this class, or by it's inner classes.
All together it is a class level constant for use only by objects of this class.
[ March 04, 2002: Message edited by: Cindy Glass ]
 
Elahe Shafie
Ranch Hand
Posts: 291
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Sindy and what is the definition of "constant"
Thanks,
ELham
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Constant: To not change. as in:
But I am as constant as the northern star,
Of whose true-fix'd and resting quality
There is no fellow in the firmament.
The skies are painted with unnumber'd sparks,
They are all fire and every one doth shine,
But there's but one in all doth hold his place
But seriously, a constant is something that cannot change. If I declare a variable as final, that means no other code can change the value of that variable. Same as in math problems. (like the value of PI is a constant)

So:

_HAPPY = "Not_Happy";

would result in a compiler error.
[ March 04, 2002: Message edited by: Mike Curwen ]
 
Elahe Shafie
Ranch Hand
Posts: 291
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much for your answer.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic