This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strange Enum

 
Aashu Mahajan
Ranch Hand
Posts: 114
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Source : Written by Me.


Output :

I could not understand why does it print 'null' in the first line of output???
 
Aashu Mahajan
Ranch Hand
Posts: 114
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One more thing i would like to ask an Enum can implements interface but can not extends class. why??
 
Tomski Simon
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 114
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Tomski Simon,
Enum doesn't allow inheritance. This thing i didn't know
Thanks

 
neelam kamboj
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but i got only following output for the given program.

main
 
Aashu Mahajan
Ranch Hand
Posts: 114
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@neelam kamboj

How it can be?? Did you properly copied he code???
 
neelam kamboj
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry my mistake..

i omitted the following line..
 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Aashu Mahajan
Ranch Hand
Posts: 114
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 394
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 114
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 394
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 114
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 394
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic