Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sadegh Aliakbary Sharif University of Technology Fall 2010.

Similar presentations


Presentation on theme: "Sadegh Aliakbary Sharif University of Technology Fall 2010."— Presentation transcript:

1 Sadegh Aliakbary Sharif University of Technology Fall 2010

2 Agenda Enumerations Inner classes Anonymous inner classes Reflection and RTTI Annotations Fall 2010Sharif University of Technology2

3

4 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 2010Sharif University of Technology4

5 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 2010Sharif University of Technology5 Applications of a private constructor

6 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 2010Sharif University of Technology6

7 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 2010Sharif University of Technology7

8 Enum Sample enum Shape { Rectangle, Circle, Square } enum StudentType{ BS, MS, PhD } Fall 2010Sharif University of Technology8

9 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 2010Sharif University of Technology9

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. Fall 2010Sharif University of Technology10

11 Enum Enum can be a more complex class With many constructors And fields And methods Fall 2010Sharif University of Technology11

12 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 2010Sharif University of Technology12

13

14 Class Types class FriendlyClass{ } public class OuterClass { private int value; public class Inner{ public void f(){ … } Fall 2010Sharif University of Technology14 Friendly Class Public class Inner Class

15 Inner Classes Declared in another class Instantiated using a reference object of outer class Has access to this object The inner class can be static No reference object of outer class is needed No access to outer class is provided Fall 2010Sharif University of Technology15

16 public class OuterClass { private int value = 2; class Inner{ public void innerMethod(){ OuterClass.this.value = 5; } public void outerMethod(){ Inner inner = new Inner(); inner.innerMethod(); } public static void main(String[] args) { OuterClass outer = new OuterClass(); System.out.println(outer.value); outer.outerMethod(); System.out.println(outer.value); } Fall 2010Sharif University of Technology16 OuterClass.this is implicitly saved in inner object

17 public class OuterClass { private int value = 2; class Inner{ public void f(){ OuterClass.this.value = 5; } public static void main(String[] args) { OuterClass outer = new OuterClass(); Inner inner = outer.new Inner(); System.out.println(outer.value); inner.f(); System.out.println(outer.value); } Fall 2010Sharif University of Technology17 Why we need outer reference?

18 class OuterClass { static class Inner{ public void f(){ System.out.println("f() invoked"); } public class MainClass { public static void main(String[] args) { OuterClass.Inner in = new OuterClass.Inner() ; in.f(); } Fall 2010Sharif University of Technology18

19 Anonymous Inner Class An inner class With no name Created once Used once No access to this class from any other place Once created and used Fall 2010Sharif University of Technology19

20 interface Inner{ void innerMethod();} public class OuterClass { private int value = 2; public void outerMethod(){ Inner inner = new Inner() { public void innerMethod() { OuterClass.this.value = 5; } }; inner.innerMethod(); } public static void main(String[] args) { OuterClass outer = new OuterClass(); System.out.println(outer.value); outer.outerMethod(); System.out.println(outer.value); } Fall 2010Sharif University of Technology20

21 Anonymous Inner Class Usually implements an interface Or extends another class And Overrides some special method Main Application : Event Handlers Fall 2010Sharif University of Technology21

22 The End Fall 2010Sharif University of Technology22


Download ppt "Sadegh Aliakbary Sharif University of Technology Fall 2010."

Similar presentations


Ads by Google