• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem in code

 
akaash singh
Ranch Hand
Posts: 45
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey everyone ,

i have a doubt in the following code :



the doubt i have is : Since static methods cannot directly access non-static members , how are we able to directly access the enum Days in the main method , without an object of the class TestDays . Are enums inside a class not considered as instance variable ?


[HENRY: added code tags]
 
Henry Wong
author
Marshal
Pie
Posts: 20820
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are enums inside a class not considered as instance variable ?


In Java, "enum" is not the type, it is a keyword to declare the type. So... days is not a variable, static or instance.

In fact, using "Days" is more similar to using a nested class than a variable (or at least, it is probably easier to think of it that way).

Henry
 
Ken Truitt
Ranch Hand
Posts: 124
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All the references refer to enums as a type of constant and that they are implicitly public, static and final--so when an enum is
defined within a class, you don't need to access it via a reference to the class. And you can refer to an enum defined
outside a class definition similar to how you would refer to a static entity of another class.
 
akaash singh
Ranch Hand
Posts: 45
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry and Ken ,

Can you also suggest some material from where i can read more about enums , as of now i have the kathy and berts book.

Regards aakash
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic