• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Regarding accessing of enum declared inside and outside a class

 
Greenhorn
Posts: 24
Android Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 115
Firefox Browser Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 24
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
Jean John
Greenhorn
Posts: 24
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear John,

There is a rule over ranch which is NoNeedToSaySorry

Mistakes happen, so it's perfectly fine.

HTH,
 
Nitesh Nandwana
Ranch Hand
Posts: 115
Firefox Browser Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 115
Firefox Browser Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 promise I will be the best, most loyal friend ever! All for this tiny ad:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic