Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

enum constructor help

 
Sun Craven
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21362
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Source " Free online mock test"


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

Thanks,
Henry
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Padmanabh Sahasrabudhe
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 336
Firefox Browser Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic