This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes enum constructor private Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "enum constructor private" Watch "enum constructor private" New topic
Author

enum constructor private

rhett howard
Greenhorn

Joined: Nov 24, 2008
Posts: 23
Hey guys,
I was wondering about the use of private constructor inside an enum which is outside the class........I am still able to initialize the object from another class, rite??

And why is public not allowed there???


package javaapplication3;

enum coffeeSize { Big(1), Small(4), Verysmall(5);
public int points;
private coffeeSize(int points)
{
System.out.println("hello");
this.points = points;
}
int getval()
{
return points;
}
}


public class Main
{


public static void main(String [] args)
{
coffeeSize size = coffeeSize.Small;
// coffeeSize cs [] = size.values();
// for(coffeeSize cl : cs)
System.out.println(size);

}
}


Output:

run:
hello
hello
hello
Small
BUILD SUCCESSFUL (total time: 0 seconds)



Thanks .


Bob Wheeler
Ranch Hand

Joined: Apr 24, 2009
Posts: 317
Firs of all, use the code brackets like this (better for my eyes ):


SCJP 6 - SCJD - SCWCD 5 - SCBCD 5
JavaEnterpriseEditionFaq - TomcatFaq
Bob Wheeler
Ranch Hand

Joined: Apr 24, 2009
Posts: 317
I was wondering about the use of private constructor inside an enum which is outside the class........I am still able to initialize the object from another class, rite??

You don't call the constructor from outside the class. If you invoke this line:

the constant Small(4) calls the constructor, so its called inside the class and everyone is happy.


And why is public not allowed there???

EDIT: sorry, that was wrong!!! doesn't work. Pete Stein is right.
[Don't know why? It works for me.]

cheers
Bob
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
rhett howard wrote:Hey guys,
I was wondering about the use of private constructor inside an enum which is outside the class........I am still able to initialize the object from another class, rite??
And why is public not allowed there???

Things are made private when they aren't supposed to be visible from outside of the class (or in this case, the enum). Enums are not supposed to be modifiable, and so you shouldn't be able to call its constructor outside of the enum. The private constructor is for the enum's protection.

More is written on this at the Sun Java tutorials, enum section: Enum Types. Please have a look.
rhett howard
Greenhorn

Joined: Nov 24, 2008
Posts: 23
Thanks Bob and Pete.
But why isn't public allowed for the constructor?
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
rhett howard wrote:But why isn't public allowed for the constructor?

1) because that's the rules according to the JLS (Java Language Specification), and
2) to prevent outside forces from calling it (as noted above).
rhett howard
Greenhorn

Joined: Nov 24, 2008
Posts: 23
Oh, okie......I didn't know constructors of enum are implicitly private.I thought it had default modifier when we dont give any modifier.....But looks like its allowed only a private modifier.

Thanks......

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: enum constructor private
 
Similar Threads
enum - What's wrong with this code?
Enums...
What is Enum?