Enum

947 views

Published on

0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
947
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
0
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Enum

  1. 1. Enum Supported by jdk1.5+
  2. 2. Enum <ul><li>An Enum is a fixed set of final references to objects. </li></ul>enum Maruti // enum type { Maruti800,Swift; // enum constants } <ul><li>Enum constants are self-typed. It means both Maruti800 & Swift are reference variables of type Maruti. </li></ul>
  3. 3. An Enum is strictly a class-type <ul><li>javap Maruti </li></ul>final class Maruti extends java.lang.Enum { public static final Maruti Maruti800; public static final Maruti Swift; public static Maruti[] values(); public static Maruti valueOf(java.lang.String); static {}; } <ul><li>Needless to say,enums are type safe. </li></ul><ul><li>Can an enum extend a class? </li></ul><ul><li>Can it be extended? </li></ul><ul><li>Can an enum implement an interface? </li></ul>
  4. 4. Enum <ul><li>enum Maruti </li></ul><ul><li>{ </li></ul><ul><li>MARUTI800(&quot;red&quot;),SWIFT; </li></ul><ul><li>private String color; </li></ul><ul><li>Maruti(String color) </li></ul><ul><li>{ </li></ul><ul><li>this.color=color; </li></ul><ul><li>System.out.println(&quot;I want &quot;+color+&quot; one.&quot;); </li></ul><ul><li>} </li></ul><ul><li>Maruti() </li></ul><ul><li>{ </li></ul><ul><li>color=&quot;gray&quot;; </li></ul><ul><li>System.out.println(&quot;No color selected&quot;) </li></ul><ul><li>} </li></ul><ul><li>public String getColor() </li></ul><ul><li>{ return color; } </li></ul><ul><li>} </li></ul>class Test { public static void main(String[]args) { Maruti m1 = Maruti.SWIFT; Maruti m2=Maruti.MARUTI800; } } Output : I want red one. No color selected
  5. 5. Points To Notice <ul><li>When constructors have been invoked? </li></ul><ul><li>Both MARUTI800 & SWIFT are static reference variables of type Maruti having references to 2 different objects of its type. It means objects will be created at the time of static allocation. </li></ul><ul><li>Think of the following.. </li></ul><ul><ul><ul><ul><li>Maruti m1; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Will this declaration in main() cause Loading of class Maruti? </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Will constructors be invoked this time? </li></ul></ul></ul></ul>
  6. 6. Think of the following.. <ul><li>public class Test </li></ul><ul><li>{ </li></ul><ul><li> static Test ref=new Test(); </li></ul><ul><li> static </li></ul><ul><li> { </li></ul><ul><li> System.out.println(&quot;Static block invoked&quot;); </li></ul><ul><li> } </li></ul><ul><li> { </li></ul><ul><li> System.out.println(&quot;non-static block invoked&quot;); </li></ul><ul><li> } </li></ul><ul><li> public Test() </li></ul><ul><li> { </li></ul><ul><li> System.out.println(&quot;Constructor invoked&quot;); </li></ul><ul><li> } </li></ul><ul><li> public static void main(String args[]) </li></ul><ul><li> { </li></ul><ul><li> System.out.println(&quot;main&quot;); </li></ul><ul><li> } </li></ul><ul><li>} </li></ul>What should be the output?? non-static block invoked Constructor invoked Static block invoked main
  7. 7. What about this.. <ul><li>public class NewClass </li></ul><ul><li>{ </li></ul><ul><li>enum Inner </li></ul><ul><li>{ </li></ul><ul><li>I1,I2; </li></ul><ul><li>static{ </li></ul><ul><li>System.out.println(“Inner”); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>static enum Nested </li></ul><ul><li>{ </li></ul><ul><li>N1,N2; </li></ul><ul><li>} </li></ul><ul><li>public static void main(String args[]) </li></ul><ul><li>{ </li></ul><ul><li>System.out.println(Inner.I1); </li></ul><ul><li>System.out.println(Nested.N1); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>

×