This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Strange Enum Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Strange Enum" Watch "Strange Enum" New topic
Author

Strange Enum

Aashu Mahajan
Ranch Hand

Joined: May 27, 2011
Posts: 113

Source : Written by Me.


Output :

I could not understand why does it print 'null' in the first line of output???
Aashu Mahajan
Ranch Hand

Joined: May 27, 2011
Posts: 113

One more thing i would like to ask an Enum can implements interface but can not extends class. why??
Tomski Simon
Greenhorn

Joined: May 30, 2011
Posts: 7
If you change

to

you will understand why there is a null in the first line of your output.

It looks like constructor is executed for each constant (A, B, C, D) of your enum. So it is not possible to print value of A constant before it is created.

About lack of inheritance for enums in java: http://www.coderanch.com/t/327915/java/java/Enum-Type-inhteritance
Aashu Mahajan
Ranch Hand

Joined: May 27, 2011
Posts: 113

Thanks Tomski Simon,
Enum doesn't allow inheritance. This thing i didn't know
Thanks

neelam kamboj
Greenhorn

Joined: Sep 18, 2008
Posts: 5
but i got only following output for the given program.

main


Thank you,
Poonam Kamboj
Aashu Mahajan
Ranch Hand

Joined: May 27, 2011
Posts: 113

@neelam kamboj

How it can be?? Did you properly copied he code???
neelam kamboj
Greenhorn

Joined: Sep 18, 2008
Posts: 5
sorry my mistake..

i omitted the following line..
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

Tomski Simon wrote:It looks like constructor is executed for each constant (A, B, C, D) of your enum. So it is not possible to print value of A constant before it is created.


Hello Tomski, I 'think' your analysis is correct, because when I used the 'this' reference to invoke the constructor the values were displayed, please observe:

Output:

The 'this' reference invokes the constructor and then the constructor evaluates the enum (A,B,C,D) values 'behind-the-scenes', returns those values to the println statement and then displays them.


OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.
Aashu Mahajan
Ranch Hand

Joined: May 27, 2011
Posts: 113

Hi Ikpefua Jacob-Obinyan
I read that 'Enum constructor can't be invoked directly'. I could not understand your statement :

The 'this' reference invokes the constructor and then the constructor evaluates the enum (A,B,C,D) values 'behind-the-scenes', returns those values to the println statement and then displays them.

Actually I could not understand the line :

And this following line also give the same output :

What is the purpose of using 'this' and 'super' here???
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

Aashu wrote:Enum constructor can't be invoked 'DIRECTLY'


Ikpefua wrote:The this reference invokes the constructor and then the constructor evaluates the enum (A,B,C,D) values 'behind-the-scenes', returns those values to the println statement and then displays them.


Did you notice the expression 'BEHIND-THE-SCENES' ?

I guess I ought to have AVOIDED using the expression 'invoke' or reconstruct my sentence in another manner I apologise for any mis-understandings

The official documentation -ORACLE- says quote:
Within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called.


You used the super keyword within an 'anonymous-sub-class' that implements the getName() method defined in the interface so my 'thoughts' is that both super AND this refer to the same thing as described above; The current object...the object whose method or constructor is being called.
Let us consider the 'enum' as the class AND the 'current object' as its values (A,B,C,D).

This above analysis is to the best of my knowledge.

regards

Ikpefua.






Aashu Mahajan
Ranch Hand

Joined: May 27, 2011
Posts: 113

Hi, Ikpefua Jacob-Obinyan
I still could not understand the code

Here where we created an object of enum 'InEn'?? and 'this' refered to which current object??
Please Please Clarify my doubts, i got very confused. May be this type of question see on real exam.


Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

Aashu wrote:May be this type of question see on real exam.


Hello Aashu, please calm down, to the best of my knowledge, the exams 'difficulty' level does NOT get to this extent, you will "normally" be tested on basic enums syntax knowledge:

-enums CANNOT extend other enums
-enums CAN be declared in a package
-enums CAN be imported by other enums from package to package
-enums can have methods, constructors, etc.
-consider enum as a 'basic' java class

That said, lets try and answer your questions
Here where we created an object of enum 'InEn'?? and 'this' refered to which current object??


Let me answer your question from a "Head-First" point of view
In java an object consists of a state (fields) and behaviour (methods).
Here we have an enum class called "InEn" whose object is "A,B,C,D"...Yes I repeat, the object is "A,B,C,D"
And when you want refer to the current running object "A,B,C,D" -within the SAME class- you use the keyword "this". "InEn.this" == "A,B,C,D".
Aashu Mahajan
Ranch Hand

Joined: May 27, 2011
Posts: 113

Hi Ikpefua Jacob-Obinyan,

Your point is correct :

In java an object consists of a state (fields) and behaviour (methods).
Here we have an enum class called "InEn" whose object is "A,B,C,D"...Yes I repeat, the object is "A,B,C,D"
And when you want refer to the current running object "A,B,C,D" -within the SAME class- you use the keyword "this". "InEn.this" == "A,B,C,D".

I got this now.
But having problem with call to 'super' here in place of 'this'.
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

Aashu wrote:But having problem with call to 'super' here in place of 'this'.


Hello Aashu,
at the moment, I dont know what the "super" means in this context, for now I see that it refers to the enum "object" that "this" refers to.
 
jQuery in Action, 2nd edition
 
subject: Strange Enum
 
Similar Threads
Question on "instanceof"
Strange Behaviour Of Wrapper Class
How many Objects are garbage collected?
Strange Behaviour Of Wrapper Class