×
STUDY HARD SO YOU CAN LIVE YOUR LIFE AS YOU WANT.
--Your friends at LectureNotes
Close

Advanced Java Programming

by Vtu RangersVtu Rangers
Type: NoteInstitute: Visvesvaraya Technological University Regional Center Offline Downloads: 421Views: 15315Uploaded: 11 months ago

Share it with your friends

Suggested Materials

Leave your Comments

Contributors

Vtu Rangers
Vtu Rangers
Advance Java and J2EE –Module 1 Module -1 Enumerations, Autoboxing, and Annotations(Metadata) Enumerations  Enumerations included in JDK 5. An enumeration is a list of named constants. It is similar to final variables.   Enum in java is a data type that contains fixed set of constants.  An enumeration defines a class type in Java. By making enumerations into classes, so it can have constructors, methods, and instance variables. An enumeration is created using the enum keyword. Ex: enum Apple { Jonathan, GoldenDel, RedDel, Winesap, Cortland } The identifiers Jonathan, GoldenDel, and so on, are called enumeration constants. Each is implicitly declared as a public, static final member of Apple. Enumeration variable can be created like other primitive variable. It does not use the new for creating object. Ex:Apple ap; Ap is of type Apple, the only values that it can be assigned (or can contain) are those defined by the enumeration. For example, this assigns: ap = Apple.RedDel; Example Code-1 enum Apple { Jonathan, GoldenDel, RedDel, Winesap, Cortland } class EnumDemo { public static void main(String args[]) { Apple ap; ap = Apple.RedDel; System.out.println("Value of ap: " + ap);// Value of ap: RedDel ap = Apple.GoldenDel; if(ap == Apple.GoldenDel) System.out.println("ap contains GoldenDel.\n"); // ap contains GoldenDel. switch(ap) { case Jonathan: System.out.println("Jonathan is red."); break; case GoldenDel: System.out.println("Golden Delicious is yellow.");// Golden Delicious is yellow
Advance Java and J2EE –Module 1 break; case RedDel: System.out.println("Red Delicious is red."); break; case Winesap: System.out.println("Winesap is red."); break; case Cortland: System.out.println("Cortland is red."); break; } } } The values( ) and valueOf( ) Methods All enumerations automatically contain two predefined methods: values( ) and valueOf( ). Their general forms are shown here: public static enum-type[ ] values( ) public static enum-type valueOf(String str) The values( ) method returns an array that contains a list of the enumeration constants. The valueOf( ) method returns the enumeration constant whose value corresponds to the string passed in str. Example Code-2: enum Season { WINTER, SPRING, SUMMER, FALL } ; class EnumExample1{ public static void main(String[] args) { for (Season s : Season.values()) System.out.println(s); Season s = Season.valueOf("WINTER"); System.out.println("S contains " + s); } }
Advance Java and J2EE –Module 1 Example Code-3 class EnumExample5{ enum Day{ SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDA Y, SATURDAY} public static void main(String args[]){ Day day=Day.MONDAY; switch(day){ case SUNDAY: System.out.println("sunday"); break; case MONDAY: System.out.println("monday"); break; default: System.out.println("other day"); } }} Class Type Enumeration enum Apple { Jonathan(10), GoldenDel(9), RedDel(12), Winesap(15), Cortland(8); private int price; Apple(int p) { price = p; } int getPrice() { return price; } }
Advance Java and J2EE –Module 1 class EnumDemo3 { public static void main(String args[]) { Apple ap; System.out.println("Winesap costs " + Apple.Winesap.getPrice() + " cents.\n"); System.out.println("All apple prices:"); for(Apple a : Apple.values()) System.out.println(a + " costs " + a.getPrice() + " cents."); } } The Class type enumeration contains three things The first is the instance variable price, which is used to hold the price of each variety of apple. The second is the Apple constructor, which is passed the price of an apple. The third is the method getPrice( ), which returns the value of price. When the variable ap is declared in main( ), the constructor for Apple is called once for each constant that is specified. the arguments to the constructor are specified, by putting them inside parentheses after each constant, as shown here: Jonathan(10), GoldenDel(9), RedDel(12), Winesap(15), Cortland(8); These values are passed to the parameter of Apple(),which then assigns this value to price. The constructor is called once for each constant. Because each enumeration constant has its own copy of price, you can obtain the price of a specified type of apple by calling getPrice(). For example, in main() the price of a Winesap is obtained by the following call:Apple.Winesap.getPrice()

Lecture Notes