• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Enum

 
Prasad Maddipatla
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 513
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Prasad Maddipatla
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Kelvin
 
Shaili Merchant
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 513
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15354
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"kssn nssk", please check your private messages. You can see them by clicking My Private Messages.
 
Shaili Merchant
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh Thanks Kelvin...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic