aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Enum accessing PRoblem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Enum accessing PRoblem" Watch "Enum accessing PRoblem" New topic
Author

Enum accessing PRoblem

Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

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

OCPJP 6.0 93%
OCPJWCD 5.0 98%
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

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.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

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")




SCJP6.0,My blog Ranchers from Delhi
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

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

Joined: Oct 25, 2008
Posts: 2700

Head spinner:
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

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

Joined: Oct 25, 2008
Posts: 2700

The point I was trying to make is that it even works on a null reference of an enum variable.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Great..
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Enum accessing PRoblem