Presentation is loading. Please wait.

Presentation is loading. Please wait.

N ESTED C LASSES -1. T YPES OF N ESTED C LASSES & I NTERFACES top-level nested classes interfaces inner classes member local named anonymous.

Similar presentations


Presentation on theme: "N ESTED C LASSES -1. T YPES OF N ESTED C LASSES & I NTERFACES top-level nested classes interfaces inner classes member local named anonymous."— Presentation transcript:

1 N ESTED C LASSES -1

2 T YPES OF N ESTED C LASSES & I NTERFACES top-level nested classes interfaces inner classes member local named anonymous

3 S OME D EFINITIONS Nested = a class or interface definition is somewhere inside another one Top-level class or interface = an instance of a type does not need to be instantiated with a reference to any enclosing instance Inner class = an instance of a class does need to be instantiated with an enclosing instance Inner Member class = defined inside another class, but not inside any methods. Inner Local class = defined inside a method Named Inner Local class = has a class name Anonymous Inner Local class = does not have a class name

4 S TATIC M EMBER C LASSES & I NTERFACES Declared inside a class (or interface) definition Must be declared as static The class (or interface) definition is not a member of an instantiation of the outer class (or interface). Shows a close functional relationship between the types. Class referencing syntax: OuterClass.InnerClass You can nest classes in interfaces, and vice versa.

5 H ERE P ROPERTY IS A S TATIC M EMBER C LASS public class PropertyValueFinder { final String GET = "get"; public void printProperties(Object o) {... } private Property propertyGottenBy(Method m) {...} private boolean isGetter(Method m) {...} static public class Property {...} } in another class, far, far away... PropertyValueFinder.Property p = new PropertyValueFinder.Property("hi", String.class); No enclosing instance of PropertyValueFinder is needed to make an instance of Property

6 S COPE OF S TATIC M EMBER C LASSES Enclosed classes share private access with other enclosed classes in the same enclosing class, and with the enclosing class. (Note: the book is wrong on this point, as shown by the compilation and running of the following example.)

7 Declaration and Instantiation

8 public class TopLevelNested { private int myIntTLN = 3; void test() { new Other1().printOther2(); new Other2().printOther1(); } private void test2() {System.out.println("test 2");} static class Other1 { private int myIntO1 = 1; static void printOther2(){ System.out.println(new Other2().myIntO2); new TopLevelNested().test2(); } private static class Other2 { private int myIntO2 = 2; private static void printOther1(){ System.out.println(new Other1().myIntO1); System.out.println(new TopLevelNested().myIntTLN); }

9 I NNER C LASSES Member Defined in class definition, not static Local (private, not static) Defined in method definition May be named or anonymous

10 U SING I NNER C LASSES Reasons to use: Nice for classes that implement 1-method interfaces Adapter classes (see ex. page. 129) Presenting some aspect(s) of the enclosing class in terms of an interface that it does not implement Can not be static. The class definition is a member of the outer class. (And therefore accessible via reflection.) Inner object shares scope of outer object. These are classes, not interfaces.

11 H ERE P ROPERTY IS A M EMBER I NNER C LASS public class PropertyValueFinder { final String GET = "get"; public void printProperties(Object o) {... } private Property propertyGottenBy(Method m) {...} private boolean isGetter(Method m) {...} public class Property {...} } in another class, far, far away... PropertyValueFinder pvf = new PropertyValueFinder(); PropertyValueFinder.Property p = pvf.new Property("hi", String.class); An enclosing instance of PropertyValueFinder is needed to make an instance of Property

12 S COPE OF I NNER C LASSES Scope: in the inner class, “this” refers to both the instance of the inner class and its enclosing class instance this means that the inner object can refer directly to fields and methods of the outer object See example next page

13 public class Outer { private String myString = "outer"; private String outerString ="outer outer !"; void test() { System.out.print("test "); System.out.println(myString); new Inner().testInner(); } private void test2() { System.out.print("test2 "); System.out.println(myString); } class Inner { private String myString = "inner"; private void testInner(){ System.out.print("testInner "); System.out.println(myString); System.out.println(outerString); test2(); }

14 I NNER CLASSES EXTEND OTHER CLASSES Inheritance vs. Enclosing Scope If not fully specified, order is: inner, super, enclosing (book says won’t compile, but I don’t find this true) If all of these exist, you should specify to: inner: this.X super: super.X enclosing: EnclosingClassName.this.X

15 E NCLOSING C LASS HAS OUTER MOST SCOPE public class ScopeConflict { String s = "outer"; class Inner extends SuperClass { //String s = "inner"; void foo(){ System.out.println(s); } class SuperClass { //String s = "super"; } in another class: ScopeConflict sc = new ScopeConflict(); ScopeConflict.Inner inner = sc.new Inner(); inner.foo(); output: outer

16 S UPER CLASS HAS SCOPE BETWEEN E NCLOSING AND I NNER CLASS public class ScopeConflict { String s = "outer"; class Inner extends SuperClass { //String s = "inner"; void foo(){ System.out.println(s); } class SuperClass { String s = "super"; } in another class: ScopeConflict sc = new ScopeConflict(); ScopeConflict.Inner inner = sc.new Inner(); inner.foo(); output: super

17 I NNER CLASS HAS INNERMOST SCOPE public class ScopeConflict { String s = "outer"; class Inner extends SuperClass { String s = "inner"; void foo(){ System.out.println(s); } class SuperClass { String s = "super"; } in another class: ScopeConflict sc = new ScopeConflict(); ScopeConflict.Inner inner = sc.new Inner(); inner.foo(); output: inner

18 S PECIFYING WHICH SCOPE YOU WANT : public class ScopeConflict { String s = "outer"; class Inner extends SuperClass { String s = "inner"; void foo(){ System.out.println(this.s); System.out.println(super.s); System.out.println(ScopeConflict.this. s); } class SuperClass { String s = "super"; } output: inner (from this.s) super (from super.s) outer (from ScopeConflict.this.s)

19 M EMBER I NNER C LASS AS S UPERCLASSES public class Computer { int model; Computer(int i) { model = i; } public class HardDrive { int size; public HardDrive(int i) { size = i; } public HardDrive() { size = 40; } class SCSI extends Computer.HardDrive{ SCSI (Computer c) { c.super(80); }

20 L OCAL I NNER C LASSES Defined inside a method in an enclosing class Has access not only to the scope of the enclosing object(s), but also to the scope of the enclosing method and any code blocks that are final. Two types Local Inner Named Classes Local Inner Anonymous Classes

21 L OCAL I NNER C LASSES In a instance method Can use all members of the enclosing instance In a class method Can only use class members (because it’s in a static context).

22 L OCAL I NNER C LASSES Why must the local enclosing fields be final to be in scope? Because the objects (not the classes) can be used outside of the scope they were created in, so they must have a copy of the local fields from their original scope. These local copies would be wrong if the fields changed, so the fields must be final to protect the integrity of the local inner class object.

23 N AMED L OCAL I NNER CLASSES public class Outer { public void outerMethod() { class InnerClass {...} } accesses all of these things from different scopes: private instance variable of enclosing object [equationInput] arguments of the instance method the local inner class was defined in [input1 & input2] private variable local to method [localVar[2]] private instance of local inner class [normalField]

24 A NONYMOUS L OCAL I NNER C LASSES syntax: Type objectName = new [SuperType()] {...} (If you don’t provide a SuperType, defaults to Object, as usual.) This syntax both defines the class and instantiates it. No name, and no constructors. To initialize instance variables, use a initialization statement: { // initializations go here }

25 S TATIC I NITIALIZER B LOCKS you can write static initializer blocks to initialize static variables when the class is loaded. The code in a static initializer block can also be quite complex. A static initializer block resembles a method with no name, no arguments, and no return type. It doesn't need a name, because there is no need to refer to it from outside the class definition. The code in a static initializer block is executed by the virtual machine when the class is loaded. Like a constructor, a static initializer block cannot contain a return statement. Therefore, it doesn't need to specify a return type. Because it is executed automatically when the class is loaded, parameters don't make any sense, so a static initializer block doesn't have an argument list. Syntax: static{ //code here }

26 N ON -S TATIC I NITIALIZER B LOCKS Normally, you would put code to initialize an instance variable in a constructor. There are two alternatives to using a constructor to initialize instance variables: initializer blocks and final methods. Initializer blocks for instance variables look just like static initializer blocks, but without the static keyword: { // whatever code is needed for initialization goes here } The Java compiler copies initializer blocks into every constructor. Therefore, this approach can be used to share a block of code between multiple constructors.


Download ppt "N ESTED C LASSES -1. T YPES OF N ESTED C LASSES & I NTERFACES top-level nested classes interfaces inner classes member local named anonymous."

Similar presentations


Ads by Google