File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes What is the definition of...... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is the definition of......" Watch "What is the definition of......" New topic

What is the definition of......

Elahe Shafie
Ranch Hand

Joined: Dec 12, 2001
Posts: 291
Dear Friends,
Can you tell me what id the definition of static here?
private final static String _HAPPY = "happy";

Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
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 ]

"JavaRanch, where the deer and the Certified play" - David O'Meara
Elahe Shafie
Ranch Hand

Joined: Dec 12, 2001
Posts: 291
Thank you Sindy and what is the definition of "constant"
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

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)


_HAPPY = "Not_Happy";

would result in a compiler error.
[ March 04, 2002: Message edited by: Mike Curwen ]
Elahe Shafie
Ranch Hand

Joined: Dec 12, 2001
Posts: 291
Thank you so much for your answer.
I agree. Here's the link:
subject: What is the definition of......
jQuery in Action, 3rd edition