wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Regarding accessing of enum declared inside and outside a class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Regarding accessing of enum declared inside and outside a class" Watch "Regarding accessing of enum declared inside and outside a class" New topic
Author

Regarding accessing of enum declared inside and outside a class

Jean John
Greenhorn

Joined: Dec 21, 2010
Posts: 23

Hi,
I found the below code in SCJP book for Java 6 Exam and I have a doubt regarding the comment that enclosing class name is required. I tried without using the enclosing class name and still it works. The jdk i have right now is 1.6



The book mentions that the syntax for accessing an enum's members depends on where the enum was declared. But even if the enum is declared inside or outside the class, I can access the enum using the same syntax. ie. without enclosing class name. Can someone please bring out some clarity regarding this statement.Thanks.
Nitesh Nandwana
Ranch Hand

Joined: Jun 07, 2011
Posts: 115

Jean John wrote:Hi,
I found the below code in SCJP book for Java 6 Exam and I have a doubt regarding the comment that enclosing class name is required. I tried without using the enclosing class name and still it works. The jdk i have right now is 1.6



The book mentions that the syntax for accessing an enum's members depends on where the enum was declared. But even if the enum is declared inside or outside the class, I can access the enum using the same syntax. ie. without enclosing class name. Can someone please bring out some clarity regarding this statement.Thanks.


why are you using class name ? that is not right way.It is not matter that enum is declared and defined outside or inside..this'll be correct always


here you're just assigning one value to size variable from enum named CoffeeSize not from class.
Jean John
Greenhorn

Joined: Dec 21, 2010
Posts: 23

why are you using class name ? that is not right way.It is not matter that enum is declared and defined outside or inside..this'll be correct always


Please find below the examples about enum in the SCJP Book..

Declaring an enum outside a class:

The preceding code can be part of a single file. (Remember, the file must be named
CoffeeTest1.java because that's the name of the public class in the file.) The
key point to remember is that an enum that isn't enclosed in a class can be declared
with only the public or default modifier, just like a non-inner class. Here's an
example of declaring an enum inside a class:


The key points to take away from these examples are that enums can be declared
as their own class, or enclosed in another class, and that the syntax for accessing
an enum's members depends on where the enum was declared.




I have noticed that both methods work and I guess that the book refers to some previous JDK where it was mandatory to access an enum declared inside a Class by usng the enclosing class name . I have tried in JDK 1.6 and both the approach would give the same result
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Dear Jean,

There is something wrong. When an enum is declared outside the class, yes it can be accessed directly and even
from the static context. But when an enum is part of a class, you have to use the enclosing class name, else it won't
compile. Just think logically, how will other class know where the enum is declared so you need to specify the class
name, when an enum is present inside the class.

HTH,


Prithvi,
My Blog, Follow me on Twitter,Scjp Tips, When you score low in mocks, Generics,Scjp Notes, JavaStudyGroup
Jean John
Greenhorn

Joined: Dec 21, 2010
Posts: 23

Hi Prithvi,
I get it now.. The compiler didnt throw an error since I accessed the enum from the same class. I am stupid





Earlier my code(Code in the Book) was like this.


So no issues with enum and the syntax to access it when it is defined inside a class and outside the class
I am sorry about wasting everyone else's time.. Sorry
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Dear John,

There is a rule over ranch which is NoNeedToSaySorry

Mistakes happen, so it's perfectly fine.

HTH,
Nitesh Nandwana
Ranch Hand

Joined: Jun 07, 2011
Posts: 115

Prithvi Sehgal wrote:Dear Jean,

But when an enum is part of a class, you have to use the enclosing class name, else it won't
compile. Just think logically, how will other class know where the enum is declared so you need to specify the class
name, when an enum is present inside the class.

HTH,


Prithvi i don't think there is need of enclosing class name, enum is inside or outside, it'll never matter as you can see above example.
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Nitesh,

Just look at the code properly. When the enum is inside another class, you need to give the name of the enclosing class.
The case you are talking about is, when enum is outside class or inside same class. We are discussing the case when enum
is part of another class. As the context changes, the using class will never know where that enum is and will complain a compile
time error.

HTH,
Nitesh Nandwana
Ranch Hand

Joined: Jun 07, 2011
Posts: 115

Prithvi Sehgal wrote:Nitesh,

Just look at the code properly. When the enum is inside another class, you need to give the name of the enclosing class.
The case you are talking about is, when enum is outside class or inside same class. We are discussing the case when enum
is part of another class. As the context changes, the using class will never know where that enum is and will complain a compile
time error.

HTH,


Yes sure that'll be absolutely correct, i thought you're talking about above examples.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regarding accessing of enum declared inside and outside a class
 
Similar Threads
enumeration
enum doubt
Enums
simple enum doubt?
Doubt regarding enum