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

Enum Variables

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,



Variable points is private to Days. How am I able to print it in main?

Thanks for your help in advance.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not that you are "printing it in main". It's that you are accessing it from the Enums class. That variable is a member of the Enums class and therefore any code in the Enums class has access to it.
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
More clearly,

You have defined a Class called "Days" that has a private instance variable. So, you need to have a method to retrieve the information you want....

take a look at the java tutorial on enums...it has a very relevant example. You need a "getter" method in your enum class

http://java.sun.com/docs/books/tutorial/java/javaOO/enum.html
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul is right. Access modifiers like private limit access to classes, not instances of those classes.
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the more relevant point, in this case, is that a private field is private to whatever top-level class it's contained within. If it's declared in a nested class ("Days" in this case), that private field is still accessible outside the nested class, but inside the containing top-level class ("Enums" in this case).

Why did Java do it this way? I don't know. It's been this way since nested classes were introduced, I think. Well, at least since JLS second edition. Nested classes had a lot of ambiguities before that.

By coincidence, we had a closely related discussion in my Scala reading group at work today. One of the differences between Scala and Java access specifiers is that in Scala, private means private to whatever class immediately contains the thing you're declaring as private. Scala's creator evidently thought this was more logical - you're scoped to the closest set of matching containing braces. But then Scala also lets your specify more esoteric options, like private[this] or private[OtherClassName] or private[some.package.name]. So I think Scala gives a more logically consistent default, while also letting you do other things if you think you need to. Meanwhile in Java, many people still don't realize that private variables can be accessed in other classes, as long as you're still in the same top-level class. It's one of those Java gotchas that occasionally bites people.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic