• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Enum accessing PRoblem

 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problem-1




if outside class

how is Title.HORROR prints HORROR in output.shouldn't a variable of type Title be made in public static void main(String arg[]) and then after assigning value it should be accessed
-----------------------------------------------
Problem-2

here enum is declared to be member of class deleteme

a)how can enum instance variable myDog be declared in main method of deleteme without object of deletme class ??
b)shouldn't it be done in the way as done with var of cats in class deleteme ??
c) cats myDog = cats.tom;
can i access myDog with class deleteme i.e.
a.myDog
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please PostRealCode. Psvm and sop will only cause confusion.

1. No Enum entries are static and can be accessed directly with the class name.
2A. The same reason.
2B. No.
2C. Try it.

Also Enums should have a capitalized name.

 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
problem 2


a)how can enum instance variable myDog be declared in main method of deleteme without object of deletme class ??

Ans..because enum declared within a class are implicitly static,so it is perfectly fine

b)shouldn't it be done in the way as done with var of cats in class deleteme ??

Ans..or you can try it.

c) cats myDog = cats.tom;
can i access myDog with class deleteme i.e. a.myDog

Ans.. No sticly no...
the objects of enum are static and final(not the objects but basicallly the refernce variable are static variable)...myDog is a refernce variable which is already been assigned to cats.tom (i.e tom) which is final.
and i have not seen two refernce variable like this.a.myDOg(where "a" refernce variable for deleteme and "myDog" is final reference variable for cats which will always be point to "tom")



 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how is Title.HORROR prints HORROR in output.shouldn't a variable of type Title be made in public static void main(String arg[]) and then after assigning value it should be accessed..





this is how enum works..
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Head spinner:
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouter Oet wrote:Head spinner:


because ans is a refernce variable which is local to main method and local variable must be initialize before use.
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The point I was trying to make is that it even works on a null reference of an enum variable.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great..
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic