This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes How are enum constructors initialised Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
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: 1050

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: 3775

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: 1050

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
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13868
    
  10

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: 1050

then are enum constructors private???
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
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: 36453
    
  15
Like most things, there is something useful in the Java™ Tutorials.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

Try the following:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How are enum constructors initialised
 
Similar Threads
confusion regardind enum and constructors
a variable is used before declaration in enum and its running ok how
Enum programs are not compiling in java5.0 compiler?
use of " this " ...