wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Static final fields and final fields Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Static final fields and final fields" Watch "Static final fields and final fields" New topic

Static final fields and final fields

Kevin Young

Joined: Sep 24, 2009
Posts: 1
Static final fields and final fields. What is the difference between the two?

Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Uhm, that the one is a static variable and the other one an instance variable ?

JDBCSupport - An easy to use, light-weight JDBC framework -
Daniel Matthews

Joined: Aug 17, 2009
Posts: 7
static members belong to the class whereas non-static members belong to the object instance.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 38087
Welcome to JavaRanch

"Static" is one of those things often not well explained. I wrote here about "Campbell's classification" and suggested when method might be made static. Anything marked static belongs to the class, not a particular object, and you can see examples if you explore the Math class. Code in a static method is prohibited from accessing instance methods or instance fields or using the keywords this and super. That is because those things all belong to the object, and anything static belongs to the class. "Class cannot access object."

Fields marked static belong to the class
  • There is one copy of the field
  • All members of the class have access to that static field.
  • All instances of the class have access to it ("object can access class").
  • There is only one copy of that static field, (still only one copy ) so access from objects is on a shared basis.
  • If one object can change a static field, then all other objects of that class "see" the change.
  • Fields marked final can be set up once and once only. Fields which are set up immediately can be used as constants, and should be static because there is no point in having multiple copies of the same information which won't change.
    Abhijit Kangale
    Ranch Hand

    Joined: Sep 17, 2009
    Posts: 39
    v1 is instance variable & v2 is class variable. class variable is one which is member variable of class but is static.
    Campbell Ritchie

    Joined: Oct 13, 2005
    Posts: 38087
    I forgot to say you usually call static members by ClassName.memberName, not objectName.memberName.
    Pratik D mehta
    Ranch Hand

    Joined: Jul 29, 2010
    Posts: 121

    Thankyou Mr Campbell . This was extremely helpful.

    Understanding is Everything - Peter Lord
    Campbell Ritchie

    Joined: Oct 13, 2005
    Posts: 38087
    You're welcome
    I agree. Here's the link: http://aspose.com/file-tools
    subject: Static final fields and final fields
    Similar Threads
    Help with Objects..
    regarding static variables
    .equals()- How it works
    Weird Static method behavior
    Help please