• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Which access modifier should you use for constants?

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have seen two different suggestions:
HFJ recommends However another source I am looking at suggests

Which is recommended? To me, it makes more sense to use coderanch, since the value is already marked final, and cannot be changed anyways. But I am a beginner programmer, so I don't know much yet.

Thanks
 
Ranch Hand
Posts: 276
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are correct, final declares the variable not to be changed, but what the access specifiers(public/private/protected) do are completely different. With use of them, you can say which other classes can access you final variable - simply put the scope of the variable.
Try a google with 'Access Specifiers Java' and in no time you can learn them.
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ameem Shaik,

Access modifier and constant are not related. Access modifier is there to give the scope of that variable. If your variable wanted to be access beyond the class/package level, use access modifier appropriately.
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since you specify it's constant

i suggest you move on with public .....Hopefully it changes with requirements.
 
Ameem Shaik
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the quick replies. It seems I misunderstood my texts, but it is clear to me now.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is no "yes or no" answer to this question. Whether you make a constant public or private depends on how it's used, and where it needs to be accessible. You shouldn't always make a constant public or always make it private.

To minimize dependencies between classes, you should by default make things private, and only make them protected or public when there is a reason to do so.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ameem Shaik wrote:To me, it makes more sense to use coderanch, since the value is already marked final, and cannot be changed anyways.



As already said, public/private and final are orthogonal. But understand this: the final keyword only ensures that the variable can be assigned exactly once: either at declaration or in a constructor. It does NOT ensure that the state of the referred object cannot be changed. So if your final variable refers to a primitive or any immutable class (eg String) it doesn't really matter. But consider, for example, that your variable refers to a JFrame. Making it public would allow another class to set the frame's size / location / visible properties and/or to add listeners like WindowListener / WindowFocusListener or worse still, to substitute the entire contents of the frame. All the final keyword would do is prevent the variable from being assigned to a new JFrame -- not much use, that.
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
use public static final if you want to access the constant outside the class and package....
private static final
 
Marshal
Posts: 80749
486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can also use package-private (default) or protected access.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic