• Post Reply Bookmark Topic Watch Topic
  • New Topic

Which access modifier should you use for constants?  RSS feed

 
Ameem Shaik
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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 public, 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
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • 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.
 
Arun C. Giridharan
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks for the quick replies. It seems I misunderstood my texts, but it is clear to me now.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ameem Shaik wrote:To me, it makes more sense to use public, 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.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use public static final if you want to access the constant outside the class and package....
private static final
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also use package-private (default) or protected access.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!