• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

enum constructor private

 
rhett howard
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Firs of all, use the code brackets like this (better for my eyes ):
 
Bob Wheeler
Ranch Hand
Posts: 317
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bob and Pete.
But why isn't public allowed for the constructor?
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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......

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic