What is the meaning of "static" in Java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • manu verma
    New Member
    • Sep 2010
    • 2

    What is the meaning of "static" in Java

    what is static
  • Oralloy
    Recognized Expert Contributor
    • Jun 2010
    • 988

    #2
    @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.

    Comment

    • shreenath
      New Member
      • Sep 2010
      • 2

      #3
      static is nothing but the constant value

      Comment

      • Oralloy
        Recognized Expert Contributor
        • Jun 2010
        • 988

        #4
        @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:
        Code:
        private final StringBuffer inBuffer = new StringBuffer();
        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.

        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

        • Dheeraj Joshi
          Recognized Expert Top Contributor
          • Jul 2009
          • 1129

          #5
          static is nothing but the constant value
          No it is not. Final keyword is used to for that.

          Regards
          Dheeraj Joshi

          Comment

          Working...