what is static
What is the meaning of "static" in Java
Collapse
X
-
Tags: None
-
@manu_verma,
The meaning of "static" depends on the context it is used in.
Have you tried reviewing how it is used from your Java books? They'll give you a good explanation.
Basically, though, it means that the memory location associated with the variable or method is statically composed - there is only one, and it is at a fixed location in memory. But that's a very abstract description.
Another idea you might consider is that static attributes have a lifespan of the enclosing class, where objects of the class have a lifespan determined by how they are used. Again, not very clear.
Then there's the concept of static inner classes. Which means that the class is named inside the outside class, but does not require a prototypical instance of the outer class in order to be instantiated.
Hopefully that helps, but it may well confuse you, too. -
@shreenath,
Ummm, I don't think so. The keyword "static" refers to how the entity is allocated. Plus, the keyword is overloaded somewhat, as a "static" class is not statically allocated, rather it is one which does not require an outer class object to be allocated.
The keyword "final" is the closest to the "constant" concept that Java has. A "final" attribute is one which is assigned at class load ("static" attributes) or object creation (others), and cannot be overridden. In that sense, the "final" attributes are constant. However the object referred to by a "final" attribute is not constant by any means. It is quite common for programmers to write code like this:
Which means that inBuffer is assigned to a StringBuffer object that cannot be replaced. However, the class can perform any actions it wants against the StringBuffer.Code:private final StringBuffer inBuffer = new StringBuffer();
Perhaps this article will help clarify things.
Of course, there is this proposal for adding "const"ness to java. However I think its flawed, in that "const" in the proposal applies to objects, but is declared for variables/attributes.Comment
-
No it is not. Final keyword is used to for that.static is nothing but the constant value
Regards
Dheeraj JoshiComment
Comment