• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

FINAL STATIC attributes

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Is there any problem in using final static attributes in Java. Is it a good practice or bad practice?
eg:
public static final String constant = "something";
Does compilation of such final attributes provide benefits or are they an overhead?
Please do reply..
Thanks,
Sam
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's no real problem - on the contrary.
Because you're using static - these are class-scope variables.
Because you're using final, and since you give a value at declaration,
these are actually constants you can use from now on.
This is actually good practice, since static makes sure that you don't have
a copy of this variable with every instance of the class you create (moreover - you can access the constant even if you have no instance of this class in your program).
Nimo.
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that final members usually follow a convention where all characters are capitalised, and words are separate by underscore. Also, I find that using the term "constant" confuses the many C/C++ programmers who incorrectly relate Java 'final' to the C/C++ 'const'. I prefer to use the term "final members". As a part-time university lecturer, I usually sort out the differences (and emphasise that they really shouldn't be related at all) for confused students.

Would be:
 
Trust God, but always tether your camel... to this tiny ad.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic