Created
October 1, 2012 13:20
-
-
Save zbigniewTomczak/3811760 to your computer and use it in GitHub Desktop.
OCJP inner classes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class OuterClass { | |
| static class StaticInnerClass {} | |
| abstract class AbstractInnerClass{} | |
| private class PrivateInnerClass {} | |
| private void privateMember() {} | |
| public class InnerClass { | |
| public void outerPrivateMemeberCaller() {privateMember();} | |
| //static methods can only be declared in a static or top level type | |
| //public static void innerClassStaticMember() {} | |
| //static fields can only be declared in a static or top level type | |
| //public static int innerStaticField; | |
| } | |
| public void innerClassInstantiation() { | |
| InnerClass inner = new InnerClass(); | |
| } | |
| public static void innerClassInstantiationFromStatic() { | |
| InnerClass inner = null; | |
| //No enclosing instance of type InnerClasses is accessible. | |
| //Must qualify the allocation with an enclosing instance of type InnerClasses | |
| //(e.g. x.new A() where x is an instance of InnerClasses) | |
| //inner = new InnerClass(); | |
| inner = new OuterClass().new InnerClass(); | |
| } | |
| public void gettingOuterClassReference() { | |
| OuterClass outer = OuterClass.this; | |
| } | |
| } | |
| class SecondClass { | |
| public void innerClassInstantiationFromOutside() { | |
| OuterClass.InnerClass inner = new OuterClass().new InnerClass(); | |
| //Cannot allocate the member type outerClass.InnerClass using its compound name when qualified by an enclosing instance. | |
| //The member type name is resolved relatively to the qualifying instance type | |
| //OuterClass.InnerClass inner2 = new OuterClass().new OuterClass.InnerClass(); | |
| //no access | |
| //OuterClass.PrivateInnerClass privateInner = null; | |
| OuterClass.StaticInnerClass staticInner = new OuterClass.StaticInnerClass(); | |
| } | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class OuterClass2 { | |
| public interface InnerInterface {} | |
| static public interface StaticInnerInterface {} | |
| private int privateFieldInt = 3; | |
| public void methodLocalClasses() { | |
| int localInt = 4; | |
| final int finalLocalInt = 5; | |
| /* only abstract or final keyword here */ | |
| class InnerClass { | |
| public void go() { | |
| //can't | |
| //int i = localInt; | |
| int j = finalLocalInt; | |
| j = privateFieldInt; | |
| OuterClass2 stillHaveOuterRef = OuterClass2.this; | |
| } | |
| } | |
| } | |
| public void anonymousInnerClasses() { | |
| //Object o = new RandomUnknownType() {}; | |
| abstract class ToExtend {} | |
| class Simple { | |
| protected void hidden(){} | |
| private void reallyHidden() {} | |
| } | |
| //The member interface SimpleInterface can only be defined inside a top-level class or interface | |
| //interface SimpleInterface {} | |
| Object o = new ToExtend() {}; | |
| Simple s = new Simple() { //creating anonymous SUBCLASS ! of Simple | |
| public void checkIfHidded() {hidden();} | |
| //public void checkIfHidded2() {reallyHidden();} | |
| }; // <-- important semicolon | |
| int localInt = 4; | |
| final int finalLocalInt = 5; | |
| InnerInterface i = new InnerInterface() { | |
| public void go() { | |
| //can't | |
| //int i = localInt; | |
| int j = finalLocalInt; | |
| j = privateFieldInt; | |
| } | |
| }; | |
| } | |
| } | |
| class ImplementingInnerInterface implements OuterClass2.InnerInterface {} | |
| class ImplementingStaticInnerInterface implements OuterClass2.StaticInnerInterface {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment