Sadegh Aliakbary Sharif University of Technology Fall 2012
Agenda Enumerations Static Import Annotation Fall 2012Sharif University of Technology2
Enumerations Suppose you have a class with a few instances Example: Student Type : SMS Status : Color : How do you implement it? The class should not be inherited The instances are limited: no further instances Fall 2012Sharif University of Technology3
An Implementation final class Color{ public static final Color Black = new Color(1); public static final Color Blue = new Color(2); public static final Color Green = new Color(3); public static final Color Red = new Color(4); private int color; private Color(int i) { this.color = i; } Fall 2012Sharif University of Technology4 Applications of a private constructor
Java enum Java introduces enumerations for this purpose Enumerated Data Type A simple class enum keyword instead of class or interface Comma seperated enum instances enum instances are constant enum Color { Black, Blue, Green, Red } Fall 2012Sharif University of Technology5
Enum enum Color { Black, Blue, Green, Red } final class Color{ public static final Color Black = new Color(); public static final Color Blue = new Color(); public static final Color Green = new Color(); public static final Color Red = new Color(); } Fall 2012Sharif University of Technology6
Enum Sample enum Shape { Rectangle, Circle, Square } enum StudentType{ BS, MS, PhD } Fall 2012Sharif University of Technology7
Using Enums Color color = Color.Black; Shape shape = Shape.Circle; show(shape, color);... private static void show ( Shape shape, Color color) { //show a shape with this color... } Fall 2012Sharif University of Technology8
Enum Characteristics enum types are implicitly final Can not be a super-class Because they declare constants that should not be modified Instances are constants enum constants are implicitly public, static and final No new instances can be created object instantiation of enum types with operator new results in a compilation error. Fall 2012Sharif University of Technology9
Enum Enum can be a more complex class With many constructors And fields And methods Fall 2012Sharif University of Technology10
enum Shape { Rectangle(1), Circle(2), Square(3); private int number; Shape(int i){ number= i; } public int getNumber(){ return number; } Shape shape = Shape.Circle; print (shape.getNumber()); shape = Shape.valueOf("Rectangle"); print(shape.getNumber()); Shape[] shapesArray = Shape.values(); for (Shape s : shapesArray) { print(s.name()); } try{ shape = Shape.valueOf("Pyramid"); }catch(Exception exp){ print("Pyramid is not included in Shape"); } Fall 2012Sharif University of Technology11
Static Import In order to access static members Qualify references with the class they came from. For example: double r = Math.sin(Math.PI * theta); static import allows unqualified access to static members without inheriting from the type containing the static members import static java.lang.Math.PI; or import static java.lang.Math.*; double r = sin(PI * theta); Fall 2012Sharif University of Technology12
Annotation An annotation is a special form of metadata Added to Java source code Classes, methods, variables, parameters and packages may be annotated Unlike Javadoc tags, Java annotations can be reflective They can be embedded in class files (byte code) May be retained by the Java VM at @ SuppressWarnings, … Fall 2012Sharif University of Technology13
Fall 2012Sharif University of Technology14