210 likes | 310 Vues
Learn about implementing enumerations in Java for limited instances without inheritance and annotations for metadata in Java source code. Understand static import for easy access to static members. Explore the ergonomy of using these advanced features.
E N D
Advanced Programming in Java SadeghAliakbary Sharif University of Technology Fall 2012
Agenda • Enumerations • Static Import • Annotation Sharif University of Technology
Enumerations • Suppose you have a class with a few instances • Example: • Student Type : <BS, MS, PhD> • SMS Status : <Sent, Delivered, Not Delivered, Not Sent> • Color : <Blue, Green, Black, Red> • How do you implement it? • The class should not be inherited • The instances are limited: no further instances Sharif University of Technology
An Implementation finalclass Color{ publicstaticfinal Color Black = new Color(1); publicstaticfinal Color Blue = new Color(2); publicstaticfinal Color Green = new Color(3); publicstaticfinal Color Red = new Color(4); privateintcolor; private Color(inti) { this.color = i; } } Applications of a private constructor Sharif University of Technology
Java enum • Java introduces enumerations for this purpose • Enumerated Data Type • A simple class • enum keyword instead of class or interface • Comma seperatedenum instances • enum instances are constant enum Color { Black, Blue, Green, Red } Sharif University of Technology
Enum enum Color { Black, Blue, Green, Red } finalclass Color{ publicstaticfinal Color Black = new Color(); publicstaticfinal Color Blue = new Color(); publicstaticfinal Color Green = new Color(); publicstaticfinal Color Red = new Color(); } Sharif University of Technology
Enum Sample enum Shape { Rectangle, Circle, Square } enumStudentType{ BS, MS, PhD } Sharif University of Technology
Using Enums Color color = Color.Black; Shape shape = Shape.Circle; show(shape, color); ... privatestaticvoid show (Shape shape, Color color) { //show a shape with this color ... } Sharif University of Technology
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. Sharif University of Technology
Enum • Enum can be a more complex class • With many constructors • And fields • And methods Sharif University of Technology
enum Shape { Rectangle(1), Circle(2), Square(3); privateintnumber; Shape(inti){ number= i; } publicintgetNumber(){ returnnumber; } } 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"); } Sharif University of Technology
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); Sharif University of Technology
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 run-time • Example • @Override, @Deprecated, @ SuppressWarnings, … Sharif University of Technology