1 / 20

Advanced Programming in Java

Advanced Programming in Java. Sadegh Aliakbary Sharif University of Technology Fall 2012. Agenda. Enumerations Static Import Annotation. Enumerations. Enumerations. Suppose you have a class with a few instances Example: Student Type : <BS, MS, PhD>

tola
Download Presentation

Advanced Programming in Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Advanced Programming in Java SadeghAliakbary Sharif University of Technology Fall 2012

  2. Agenda • Enumerations • Static Import • Annotation Sharif University of Technology

  3. Enumerations

  4. 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

  5. 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

  6. 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

  7. 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

  8. Enum Sample enum Shape { Rectangle, Circle, Square } enumStudentType{ BS, MS, PhD } Sharif University of Technology

  9. 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

  10. 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

  11. Enum • Enum can be a more complex class • With many constructors • And fields • And methods Sharif University of Technology

  12. 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

  13. 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

  14. 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

  15. Ergonomy

  16. Sharif University of Technology

  17. Sharif University of Technology

  18. Sharif University of Technology

  19. Sharif University of Technology

  20. Sharif University of Technology

More Related