aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes enum constructor help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "enum constructor help" Watch "enum constructor help" New topic
Author

enum constructor help

Sun Craven
Greenhorn

Joined: Mar 13, 2008
Posts: 27
given the following test
enum AccountType {
SAVING, FIXED, CURRENT;
private AccountType() {
System.out.println("It is a account type");
}
}
class EnumOne {
public static void main(String[] args) {
System.out.println(AccountType.FIXED);
}
}
the answer is :Compiles fine and output is prints "It is a account type" thrice followed by "FIXED
quto source :http://www.javabeat.net/javabeat/scjp5/mocks/scjp_5_0_mock_questions_enums_part2.php
So i confused by when the enum constructor run..
Thanks
sun jiuzhen
[ March 13, 2008: Message edited by: sun jiuzhen ]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19066
    
  40

Source " Free online mock test"


Please Quote Your Sources. We need a recognizable source, or a link.

Thanks,
Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1836
    
    7

An enum type's constructor will run once for every constant it defines when the enum type is first used. For instance:



Try playing around with it.


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Padmanabh Sahasrabudhe
Ranch Hand

Joined: Mar 04, 2008
Posts: 53
Thanks Jelle. It answers. I just added One more SOP and the output confirmed me that.


Output:
C:\>java EnumExample
Invoked for type: A
Invoked for type: B
A
B
A
Milton Ochoa
Ranch Hand

Joined: Oct 23, 2007
Posts: 336

Padmanabh, sorry by contactin in this way, but you askme something via email, but Im unavailable to contact you via email or private mensaje!.
so.... sorry for no give you a answer!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: enum constructor help