Forums Register Login

Enum accessing PRoblem

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.

+Pie Number of slices to send: Send
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")



+Pie Number of slices to send: Send
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..
1
+Pie Number of slices to send: Send
Head spinner:
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
The point I was trying to make is that it even works on a null reference of an enum variable.
+Pie Number of slices to send: Send
Great..
Hug your destiny! And hug this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 795 times.
Similar Threads
how to get a collection object in spring
Abstract classs
can someone explain to me why the output would say null?
Usage of Enum...
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 07:10:07.