• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Got confused with enumerations in java

 
Ranch Hand
Posts: 67
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


OUTPUT : Age of Viraaj is 9 years

Please anyone explain me about the flow of this program ? i know that the enumerations contains only constants.... But i don't know the flow of program.Please kindly help me to understand. Thanks in advance
 
Author
Posts: 285
12
Scala IntelliJ IDE Netbeans IDE Python Java Ubuntu Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You didn't post your code accurately.

1) line 5 can't compile as it declares a method without using parentheses
2) You can't have a public constructor in an enum

Also, this is a very poor design choice to use enum values to store information about people. Enums are intended for when the concept being modeled has a fixed number of values, and those values are known at compilation time. Suits of cards (Hearts, Diamonds...) are one of the classic examples.

That said, approximately what seems to be "supposed" to happen here is:

1) Main class will load
2) main method refers to the enum class, forcing it to load
3) loading the enum creates all instances that are defined by it, initializing them through their constructors using the provided arguments
4) main method is then able to complete access to sub-expression Student.Viraaj obtaining a reference to one of the enum elements
5) main continues evaluating the expression Student.Viraaj.getAge() by calling the method on the reference obtained in 4.

HTH
Simon
 
vinoth vino
Ranch Hand
Posts: 67
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

vinoth vino wrote:

OUTPUT : Age of Viraaj is 9 years

Please anyone explain me about the flow of this program ? i know that the enumerations contains only constants.... But i don't know the flow of program.Please kindly help me to understand. Thanks in advance

output2.png
[Thumbnail for output2.png]
 
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

vinoth vino wrote:

OUTPUT : Age of Viraaj is 9 years

Please anyone explain me about the flow of this program ? i know that the enumerations contains only constants.... But i don't know the flow of program.Please kindly help me to understand. Thanks in advance



Firstly what you have there is an enum, not an enumeration. I make the distinction because there is a legacy Enumeration class in Java and it would be easy to get confused.

The flow is quite simple. When the Student enum is loaded by the classloader each of the enum members is created. Each one of those members calls the constructor for Student and supplies an age as parameter. The result is that the Student enum contains 4 different members each with a different age.

You then select one of the members and get its age by calling the getAge method, then print it out.
 
vinoth vino
Ranch Hand
Posts: 67
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I m learning this from studytonight site.In that site they declared public access modifiers in constructors is this wrong ? is enumerations will not accept puplic constructors ?
 
vinoth vino
Ranch Hand
Posts: 67
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

vinoth vino wrote:

vinoth vino wrote:

OUTPUT : Age of Viraaj is 9 years

Please anyone explain me about the flow of this program ? i know that the enumerations contains only constants.... But i don't know the flow of program.Please kindly help me to understand. Thanks in advance

 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try and compile it and see what the compiler says.
 
vinoth vino
Ranch Hand
Posts: 67
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is the variable also needs to be declared as private like constructors???
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic