| Author |
Enum
|
Prasad Maddipatla
Greenhorn
Joined: Apr 20, 2007
Posts: 24
|
|
can you help out. Program A: enum cofeesize { BIG("8"), HIGH("12"), OVER("34") { public String getLid() { return "A"; } }; private String ounces; cofeesize(String ounces) { this.ounces = ounces; } public String getLid() { return "B"; } } public class Cofee { cofeesize size; public static void main(String args[]) { Cofee d = new Cofee(); d.size = cofeesize.BIG; Cofee d1 = new Cofee(); d1.size = cofeesize.OVER; System.out.println(d.size.getLid()); System.out.println(d1.size.getLid()); System.out.println(d1.size.ounces); } } I get error when printing d1.size.ounces as onces is private and enum is outside the class Cofee. Program B: public class Cofee { cofeesize size; enum cofeesize { BIG("8"), HIGH("12"), OVER("34") { public String getLid() { return "A"; } }; private String ounces; cofeesize(String ounces) { this.ounces = ounces; } public String getLid() { return "B"; } } public static void main(String args[]) { Cofee d = new Cofee(); d.size = cofeesize.BIG; Cofee d1 = new Cofee(); d1.size = cofeesize.OVER; System.out.println(d.size.getLid()); System.out.println(d1.size.getLid()); System.out.println(d1.size.ounces); } } Here i declared enum in the class cofee and get the output without any errors. I didnot understand as we consider enum as a class then a private instance variable can be acessable from outside enum?
|
 |
Kelvin Chenhao Lim
Ranch Hand
Joined: Oct 20, 2007
Posts: 513
|
|
Originally posted by Prasad Maddipatla: I didnot understand as we consider enum as a class then a private instance variable can be acessable from outside enum?
Hi Prasad, Private fields and methods are accessible anywhere within the top-level class enclosing them. This means that a private member in an inner class is accessible to the outer class, which is what happens in your second program. More interestingly, this also means that different inner/nested classes of the same top-level class can access each other's private members. For example, you may be surprised to learn that this program is perfectly acceptable:
|
SCJP 5.0
|
 |
Prasad Maddipatla
Greenhorn
Joined: Apr 20, 2007
Posts: 24
|
|
|
Thank you Kelvin
|
 |
Shaili Merchant
Greenhorn
Joined: Nov 20, 2007
Posts: 27
|
|
Hello.. I have jus started with preparing fro SCJP 1.5... According to whatever I have understood from Chap 8 of K&B Bar is a static nested class and it can be instantiated within static method as Test.Bar b=new Test.Bar(); Please help me to solve this doubt
|
 |
Kelvin Chenhao Lim
Ranch Hand
Joined: Oct 20, 2007
Posts: 513
|
|
Hi "kssn nssk", If you needed to use Bar outside of Test, then yes you'd need to include the outer class name as a prefix. However, since the above code uses Bar within Test itself, the prefix can be omitted. (But it's still fine to put it in.)
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12929
|
|
|
"kssn nssk", please check your private messages. You can see them by clicking My Private Messages.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Shaili Merchant
Greenhorn
Joined: Nov 20, 2007
Posts: 27
|
|
|
Oh Thanks Kelvin...
|
 |
 |
|
|
subject: Enum
|
|
|