Java 1 1 Extensions Nested classes static CS














![Enumeration my. Enumerate(final Object[] arr) { class E implements Enumeration { int count = Enumeration my. Enumerate(final Object[] arr) { class E implements Enumeration { int count =](https://slidetodoc.com/presentation_image_h2/1f643c8ab8615c28dc1202dbb0903028/image-15.jpg)
![import java. util. *; class XYZ { int i; Enumeration my. Enumerate(final Object[]arr) { import java. util. *; class XYZ { int i; Enumeration my. Enumerate(final Object[]arr) {](https://slidetodoc.com/presentation_image_h2/1f643c8ab8615c28dc1202dbb0903028/image-16.jpg)




![class Local. Var. Threads { public static void main(final String[] args) { for (int class Local. Var. Threads { public static void main(final String[] args) { for (int](https://slidetodoc.com/presentation_image_h2/1f643c8ab8615c28dc1202dbb0903028/image-21.jpg)







- Slides: 28

Java 1. 1 Extensions Nested classes static CS 884 (Prasad) public/protected/private java 11 Extn 1

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. CS 884 (Prasad) java 11 Extn 2

public class Linked. List { // static by default public interface Linkable { public Linkable get. Next(); public void set. Next(Linkable node); } Linkable head; public void insert(Linkable node) {. . . } public void remove(Linkable node) {. . . } } CS 884 (Prasad) java 11 Extn 3

class Linkable. Int implements Linked. List. Linkable { int i; public Linkable. Int(int _i){i= _i; } Linked. List. Linkable next; public Linked. List. Linkable get. Next() {return next; } public void set. Next(Linked. List. Linkable node) {next = node; } } CS 884 (Prasad) java 11 Extn 4

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. CS 884 (Prasad) java 11 Extn 5

public class Bank. Account{ private long number; private int balance; private Action last. Act; public class Action { private String act; private int amount; Action(String a, int amt) { act = number + “: ” + a; amount = amt; } } public void balance last. Act } deposit(int amt) { += amt; = new Action(“deposit”, amt); withdraw(int amt) { -= amt; = new Action(“withdraw”, amt); } CS 884 (Prasad) java 11 Extn 6

public class Bank. Account{ private long number; private int balance; private Action last. Act; public class Action { … } public void deposit(int amt) { … } public void withdraw(int amt) { … } public void transfer(Bank. Account ac, int amt) { ac. withdraw(amt); deposit(amt); last. Act = new Action(“transfer”, amt); ac. last. Act = ac. new Action(“transfer”, amt); } } CS 884 (Prasad) java 11 Extn 7

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 print. Names() {…} } } public static void main(String[] a){…} } CS 884 (Prasad) java 11 Extn 8

// class C System. out. println(name); System. out. println(this. name); System. out. println(B. this. name); System. out. println(A. this. name); public void print. Names() { // // c c b 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. print. Names(); } CS 884 (Prasad) java 11 Extn 9

javap -p A$B$C public class A. B. C extends java. lang. Object { /* ACC_SUPER bit NOT set */ private final A. B this$1; public java. lang. String name; public A. B. C(A. B); public void print. Names(); } CS 884 (Prasad) java 11 Extn 10

javap -p A. B public class A. B extends java. lang. Object { private final A this$0; public java. lang. String name; public A. B(A); static A access$0(A. B); // related to accessing private {. . . A. B. C. . . } } CS 884 (Prasad) java 11 Extn 11

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. CS 884 (Prasad) java 11 Extn 12

Pathological cases • Cyclic Inheritance: illegal class A extends A. B { int i = 0; • 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(); } } class B { public static void main(String[] args) A a = new A(); B b = a. new B(); C c = new C(a); int i = 1; } } CS 884 (Prasad) { } } java 11 Extn 13

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. CS 884 (Prasad) java 11 Extn 14
![Enumeration my Enumeratefinal Object arr class E implements Enumeration int count Enumeration my. Enumerate(final Object[] arr) { class E implements Enumeration { int count =](https://slidetodoc.com/presentation_image_h2/1f643c8ab8615c28dc1202dbb0903028/image-15.jpg)
Enumeration my. Enumerate(final Object[] arr) { class E implements Enumeration { int count = 0; public boolean has. More. Elements() { return count < arr. length; } public Object next. Element() { return arr[count++]; } } return new E(); } – Scope of arr is the body of my. Enumerate function. – Lifetime of the array instance referenced by arr should be at least as long as the Enumeration object. CS 884 (Prasad) java 11 Extn 15
![import java util class XYZ int i Enumeration my Enumeratefinal Objectarr import java. util. *; class XYZ { int i; Enumeration my. Enumerate(final Object[]arr) {](https://slidetodoc.com/presentation_image_h2/1f643c8ab8615c28dc1202dbb0903028/image-16.jpg)
import java. util. *; class XYZ { int i; Enumeration my. Enumerate(final Object[]arr) { class E implements Enumeration { int count = i; public boolean has. More. Elements() { return count < arr. length; } public Object next. Element() { return arr[count++]; } } return new E(); } } CS 884 (Prasad) java 11 Extn 16

javap -p XYZ$1$E class XYZ$1$E extends java. lang. Object implements java. util. Enumeration { private final java. lang. Object val$arr[]; int count; XYZ$1$E(java. lang. Object[], XYZ); public boolean has. More. Elements(); public java. lang. Object next. Element(); } CS 884 (Prasad) java 11 Extn 17

public class Weird { // cf. closure interface Int. Holder { int get. Value(); } public static void main(String[] args) { Int. Holder[] holders = new Int. Holder[10]; for(int i=0; i<10; i++) { final int fi = i; class My. Int. Holder implements Int. Holder { public int get. Value() { return fi; } } holders[i] = new My. Int. Holder(); } // My. Int. Holder invisible here for(int i=0; i<10; i++) System. out. println(holders[i]. get. Value()); } } CS 884 (Prasad) java 11 Extn 18

Anonymous Class • Local class without a name. • Combines class definition and instantiation. • No constructor. • Can implement an interface (by implicitly extending class Object). new <class-name> ( <arg-list> ) { <class-body> } new <interface-name> ( ) { <class-body> } CS 884 (Prasad) java 11 Extn 19

import java. applet. *; import java. awt. event. *; public class Scribble extends Applet { public void init() { this. add. Mouse. Motion. Listener( new Mouse. Motion. Adapter() { public void mouse. Dragged(Mouse. Event e) { Graphics g = get. Graphics(); int x = e. get. X(), y = e. get. Y(); g. draw. Line(0, 0, x, y); } } ); }} CS 884 (Prasad) java 11 Extn 20
![class Local Var Threads public static void mainfinal String args for int class Local. Var. Threads { public static void main(final String[] args) { for (int](https://slidetodoc.com/presentation_image_h2/1f643c8ab8615c28dc1202dbb0903028/image-21.jpg)
class Local. Var. Threads { public static void main(final String[] args) { for (int i = 0; i < args. length; i++) { final int ii = i; Runnable r = new Runnable() { public int count; public void run() { System. out. println("<<"+ii+">> "+count++); }; } new Thread(r). start(); } }} CS 884 (Prasad) java 11 Extn 21

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. TYPE, etc • In Java 1. 1, class is a keyword and a field. CS 884 (Prasad) java 11 Extn 22

Reflection Introspection Meta-programming CS 884 (Prasad) java 11 Extn 23

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. CS 884 (Prasad) java 11 Extn 24

· 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. CS 884 (Prasad) java 11 Extn 25

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 Java. Bean. • customizing a component to running a method. CS 884 (Prasad) java 11 Extn 26

(cont’d) • 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 CS 884 (Prasad) java 11 Extn 27

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. CS 884 (Prasad) java 11 Extn 28