IntroToInnerClasses

IntroToInnerClasses - Java Inner Classes A Java Source File...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Java Inner Classes A Java Source File can contain more than one class Packaging of the source code for two or more classes in the same .java file is allowed by the Java compiler. (This alone does NOT create "inner classes". ) If one class is closely associated with and used by another class, and particularly if the second class is small, a Java programmer might put the two classes in the same source file. So there would be one .java file, but the compiler would create two .class files, just as if the second class had been in a separate source file. Thus the second class is "hidden" from view - you wouldn't see the class name if you looked at a directory of .java source files. (If you found the "hidden" class in a directory of .class files, you could run the javap utility on the .class file to see what .java file the source was in.) The sample source code below could be in a single file called SingleLinkedList.java. The second (and all other) class in the same source file cannot be declared public . It/They must declare accessability of package by leaving any accessability keyword off the class declaration line. (The assumption is that if these classes are so closely related as to be in the same source file,"hiding" the classname of all but the first class, then they should be in the same package (the same directory at run time). public class SingleLinkedList { private SingleNode head; // top of list private SingleNode tail; // bottom of list public void add(Object obj) { SingleNode newNode = new SingleNode(obj); tail.next = newNode; tail = newNode; } ... } // end of class SingleLinkedList class SingleNode // An "imbedded" class { public Object objectInTheList; public SingleNode next; public SingleNode(Object obj) // constructor {
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
objectInTheList = obj; } }
Background image of page 2
There is no particular term for this kind of source structure in Java. Might we call the other classes in the SingleLinkedList.java file "hidden" classes, or "imbedded" classes? What Is An Inner Class ? An inner class is declared inside another class. That is, a class called Inner could be declared inside the braces containing another class called Outer. (There are no particular restrictions or code styles for inner/outer class names.) When the source code file is compiled, two .class files are generated: the outer class file name is Outer.class , and the inner class file name is Outer$Inner.class . Two objects exist at run time. There is no awareness of an "inner object" at run time. Objects are not instantiated inside each other! All objects must be explicitly instantiated with new - the instantiation of a class does not automatically instantiate objects from inner classes it may have declared. Why Would I Use An Inner Class ?
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/25/2010 for the course ECE 309 taught by Professor Bowman during the Spring '09 term at N.C. State.

Page1 / 8

IntroToInnerClasses - Java Inner Classes A Java Source File...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online