Java 1.1 Extensions - PowerPoint PPT Presentation

About This Presentation
Title:

Java 1.1 Extensions

Description:

Static Top-level Classes and Interfaces. Like ordinary package ... public class Scribble extends Applet { public void init() { this.addMouseMotionListener ... – PowerPoint PPT presentation

Number of Views:15
Avg rating:3.0/5.0
Slides: 29
Provided by: csWr
Learn more at: http://cecs.wright.edu
Category:

less

Transcript and Presenter's Notes

Title: Java 1.1 Extensions


1
Java 1.1 Extensions
  • Nested classes
  • static public/protected/private

2
Nested classes
  • Static Top-level Classes and Interfaces
  • Like ordinary package member except for its name.
  • May use static members of its enclosing class.
  • Member Classes
  • Each instance has an enclosing instance.
  • May use members of its enclosing classes.
  • Local Classes
  • Defined in a block of code.
  • May also use final local variables and
    parameters.
  • Anonymous classes
  • One instance only.

3
  • public class LinkedList
  • // static by default
  • public interface Linkable
  • public Linkable getNext()
  • public void setNext(Linkable node)
  • Linkable head
  • public void insert(Linkable node) ...
  • public void remove(Linkable node) ...

4
  • class LinkableInt implements
  • LinkedList.Linkable
  • int i
  • public LinkableInt(int _i)i _i
  • LinkedList.Linkable next
  • public LinkedList.Linkable
  • getNext() return next
  • public void setNext(LinkedList.Linkable node)
  • next node

5
Member Class
  • Every instance of a member class is internally
    associated with an instance of the enclosing
    class.
  • The methods of a member class can implicitly
    refer to the fields defined within the member
    class, as well as those defined by any enclosing
    class (including private fields.)
  • static final members permitted.

6
  • public class BankAccount
  • private long number
  • private int balance
  • private Action lastAct
  • public class Action
  • private String act
  • private int amount
  • Action(String a, int amt)
  • act number a amount
    amt
  • public void deposit(int amt)
  • balance amt
  • lastAct new Action(deposit,amt)
  • public void withdraw(int amt)
  • balance - amt
  • lastAct new Action(withdraw,amt)

7
  • public class BankAccount
  • private long number
  • private int balance
  • private Action lastAct
  • public class Action
  • public void deposit(int amt)
  • public void withdraw(int amt)
  • public void transfer(BankAccount ac, int amt)
  • ac.withdraw(amt)
  • deposit(amt)
  • lastAct new Action(transfer,amt)

8
Scope of Fields
  • public class A
  • public String name"a"
  • public class B
  • public String name"b"
  • public class C
  • public String name"c"
  • public void printNames()
  • public static void main(String a)

9
  • public void printNames() // class C
    System.out.println(name) // c
    System.out.println(this.na
    me) // c
    System.out.println(B.this.name) // b
    System.out.println(A.this.name) //
    a
  • public static void main(String a) //class A
  • A a new A()
  • B b a.new B()
  • A.B.C c b.new C()
  • c.printNames()

10
javap -p ABC
  • public class A.B.C extends java.lang.Object
  • / ACC_SUPER bit NOT set /
  • private final A.B this1
  • public java.lang.String name
  • public A.B.C(A.B)
  • public void printNames()

11
javap -p A.B
  • public class A. B extends java.lang.Object
  • private final A this0
  • public java.lang.String name
  • public A.B(A)
  • static A access0(A.B)
  • // related to accessing private
  • ...A.B.C...

12
Scope vs Inheritance
  • Top-level class can extend a member class.
  • Class hierarchy
  • superclass to subclass
  • Containment hierarchy
  • containing class to nested class
  • Whenever there is a name conflict between an
    inherited member and a member of an enclosing
    class, Java requires explicit disambiguation.
  • The name of a member class must differ from the
    name of the enclosing package or class.

13
Pathological cases
  • Static subclass of a member class legal
  • class A int i 0
  • class B int i 1
  • static class C extends B
  • int i 2
  • C(A a) a.super()
  • public static void main(String args)
  • A a new A()
  • B b a.new B()
  • C c new C(a)
  • Cyclic Inheritance illegal

class A extends A.B int i 0 class
B int i 1
14
Local Class (vs Member class)
  • A local class is similar to a member class except
    that its methods can also access final local
    variables, final formal method parameters, and
    final exception parameters.
  • An instance of a local class has private fields
    to hold values of final local variables, final
    exception parameter, and final method parameters,
    in addition to a private reference to the
    enclosing class instance.

15
  • Enumeration myEnumerate(final Object arr)
  • class E implements Enumeration
  • int count 0
  • public boolean hasMoreElements()
  • return count lt arr.length
  • public Object nextElement()
  • return arrcount
  • return new E()
  • Scope of arr is the body of myEnumerate function.
  • Lifetime of the array instance referenced by arr
    should be at least as long as the Enumeration
    object.

16
  • import java.util.
  • class XYZ
  • int i
  • Enumeration myEnumerate(final Objectarr)
  • class E implements Enumeration
  • int count i
  • public boolean hasMoreElements()
  • return count lt arr.length
  • public Object nextElement()
  • return arrcount
  • return new E()

17

javap -p XYZ1E
  • class XYZ1E extends java.lang.Object implements
    java.util.Enumeration
  • private final java.lang.Object valarr
  • int count
  • XYZ1E(java.lang.Object, XYZ)
  • public boolean hasMoreElements()
  • public java.lang.Object nextElement()

18
  • public class Weird // cf. closure
  • interface IntHolder int getValue()
  • public static void main(String args)
  • IntHolder holders new IntHolder10
  • for(int i0 ilt10 i)
  • final int fi i
  • class MyIntHolder implements IntHolder
  • public int getValue() return fi
  • holdersi new MyIntHolder()
  • // MyIntHolder invisible here
  • for(int i0 ilt10 i)
  • System.out.println(holdersi.getValue())

19
Anonymous Class
  • Local class without a name.
  • Combines class definition and instantiation.
  • No constructor.
  • Can implement an interface (by implicitly
    extending class Object).
  • new ltclass-namegt ( ltarg-listgt )
  • ltclass-bodygt
  • new ltinterface-namegt ( )
  • ltclass-bodygt

20
  • import java.applet.
  • import java.awt.
  • import java.awt.event.
  • public class Scribble extends Applet
  • public void init()
  • this.addMouseMotionListener(
  • new MouseMotionAdapter()
  • public void mouseDragged(MouseEvent e)
  • Graphics g getGraphics()
  • int x e.getX(), y e.getY()
  • g.drawLine(0, 0, x, y)
  • )

21
  • class LocalVarThreads
  • public static void main(final String args)
  • for (int i 0 i lt args.length i)
  • final int ii i
  • Runnable r new Runnable()
  • public int count
  • public void run()
    System.out.println("ltlt"ii"gtgt
  • "count)
  • new Thread(r).start()

22
Other new features
  • Instance Initializers
  • similar to class initializers
  • serves as a constructor in anonymous classes
  • Blank finals
  • assign only once prior to use instead of in decl.
  • Anonymous arrays
  • int a a new int 1,2,3,4
  • Class literals
  • String.class, Integer.class, Integer.TYPE, etc
  • In Java 1.1, class is a keyword and a field.

23
Reflection
  • Introspection
  • Meta-programming

24
What does Reflection API do?
  • Java Runtime Environment maintains an
    immutable Class object that contains information
    about a class. Reflection API enables a program
    to
  • Determine the class of an object.
  • Get information about a class's modifiers,
    fields, methods, constructors, and superclasses.
  • Find out what constants and method declarations
    belong to an interface.

25
  • Create an instance of a class whose name is not
    known until runtime.
  • Get and set the value of an object's field, even
    if the field name is unknown to your program
    until runtime.
  • Invoke a method on an object, even if the method
    is not known until runtime.
  • Create a new array, whose size and component type
    are not known until runtime, and then modify the
    array's components.

26
Applications Construction of development tools.
  • Tools that need to discover and use public
    members of a target object based on its class.
  • GUI builders
  • selecting and creating a component.
  • determining properties, methods, events, etc
    supported by a JavaBean.
  • customizing a component to running a method.

27
(contd)
  • Tools that need to discover and use members
    declared in a given class.
  • Class browsers
  • requires information about classes to display
    their fields, methods, constructors, etc.
  • Debuggers
  • accessing and modifying field values.
  • Interpreters

28
Java Core Reflection APIs
  • package java.lang.reflect
  • new interface Member
  • new classes Field, Method, Constructor.
  • new class Array
  • new utility class Modifier
  • package java.lang
  • new wrapper classes Byte, Short
  • updated class Class
  • new objects instances of class Class to
    represent primitive types.
Write a Comment
User Comments (0)
About PowerShow.com