File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Enum 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" Watch "Enum" New topic
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
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

"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...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Enum
 
Similar Threads
use of " this " ...
confusion regardind enum and constructors
Enum overloaded constructer