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 and inner class

 
susan waters
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I am studying enum and getting lots of questions.

I can declare a class in an enum. but my question is "how can I instantiate that class" ?



How can I instantiate a class that is declared in an enum ?

Thanks for the reply
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by susan waters:
How can I instantiate a class that is declared in an enum ?


You need to create an instance of myenum by myenum.TEST or by myenum.SAMPLE or by myenum.ENGLISH. All will return you an instance of myenum but remember different instances of myenum.



Since Testing class is an inner class, so you require an instance of outer class like above.

If Testing is a static nested class, then you don't need any myenum instance and you code will work.



Naseem
[ July 27, 2006: Message edited by: Naseem Khan ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic