aspose file tools
The moose likes Beginning Java and the fly likes How are enum constructors initialised Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of Mongo DB Applied Patterns this week in the MongoDB forum
or a resume review from Five Year Itch in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "How are enum constructors initialised " Watch "How are enum constructors initialised " New topic
Author

How are enum constructors initialised

Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 969

are enums constructors intialised automatically without the creation of objects

example: enum Coffee
{
BIG(10),MEDIUM(20),LARGE(30);
Coffee(int ounces)
{
this.ounces=ounces;
}
private int ounces;

http://www.lifesbizzare.blogspot.com || OCJP:81%
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3670

Vishal Hegde wrote:are enums constructors intialised automatically without the creation of objects

Enum has a consructor which will be invoked by the enum declaration in your code (programmers cannot invoke it).


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 969

ok are enum constructors private or can it contain all the access modifier
Hariharan Ram Kumar
Greenhorn

Joined: Oct 19, 2009
Posts: 3
Enum constructors can contain any modifiers.
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12907
    
    3

Hariharan Ram Kumar wrote:Enum constructors can contain any modifiers.

No they can't. Did you try it out? If you try making the constructor of an enum public or protected, you'll get a compiler error.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 969

then are enum constructors private???
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32599
    
    4
Vishal Hegde wrote:then are enum constructors private???
As far as I know, yes. In the Java Language Specification it doesn't say a lot about enum constructors, only that a default enum constructor is private.
Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
Could you tell the use case for invoking enum construtors?
One way of looking enum is like a set of known constants. Hence I am curious to know the use case of 'constructing constants'.


Thanks and Regards
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32599
    
    4
Like most things, there is something useful in the Java™ Tutorials.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19214

Try the following:


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: How are enum constructors initialised
 
Similar Threads
a variable is used before declaration in enum and its running ok how
use of " this " ...
confusion regardind enum and constructors
Enum programs are not compiling in java5.0 compiler?