Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Static final fields and final fields

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static final fields and final fields. What is the difference between the two?



 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Uhm, that the one is a static variable and the other one an instance variable ?
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
static members belong to the class whereas non-static members belong to the object instance.
 
Marshal
Posts: 76490
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
     
    Ranch Hand
    Posts: 40
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    v1 is instance variable & v2 is class variable. class variable is one which is member variable of class but is static.
     
    Campbell Ritchie
    Marshal
    Posts: 76490
    366
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I forgot to say you usually call static members by ClassName.memberName, not objectName.memberName.
     
    Ranch Hand
    Posts: 121
    Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thankyou Mr Campbell . This was extremely helpful.
     
    Campbell Ritchie
    Marshal
    Posts: 76490
    366
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You're welcome
     
    I'm thinking about a new battle cry. Maybe "Not in the face! Not in the face!" Any thoughts tiny ad?
    The trailboss has a kickstarter
    https://coderanch.com/t/754577/Garden-Master-kickstarter
    reply
      Bookmark Topic Watch Topic
    • New Topic