File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
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: 1056

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

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

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: 14433
    
  23

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 8 API documentation
Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1056

then are enum constructors private???
Campbell Ritchie
Sheriff

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

Joined: Oct 27, 2005
Posts: 19785
    
  20

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