This week's book giveaway is in the General Computing forum.
We're giving away four copies of Emmy in the Key of Code and have Aimee Lucido on-line!
See this thread for details.
Win a copy of Emmy in the Key of Code this week in the General Computing forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

Enum

 
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?
 
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
 
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.)
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • 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...
 
She said she got a brazillian. I think owning people is wrong. That is how I learned ... tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!