Inner Classes
Inner classes let you define one class within another.
They provide a type of scoping for your classes since you can make one class a member of another class.
Just as classes have member variables and methods, a class can also have member classes.
class Outer {
/class Inner { }
}// End of class
if you compile it,
%javac Outer.java
you’ll end up with two class files:
Outer.class
Outer$Inner.class
e.g.
class Outer {
private int x = 5;
public void outerMethod() {
Inner in = new Inner();
in.innerMethod();
}
class Inner {
public void innerMethod() {
System.out.println("Outer variable x is " + x);
}
}
}// End of class
You can create four different types of inner classes, based upon the how and the where of creation.
Nested top-level classes
If you declare a class within a class and specify the static modifier, the compiler treats the class just like any other top-level class. Any class outside the declaring class accesses the nested class with the declaring class name acting similarly to a package. eg, outer.inner. Top-level inner classes implicitly have access only to static variables. There can also be inner interfaces. All of these are of the nested top-level variety.
Member classes
Member inner classes are just like other member methods and member variables and access to the member class is restricted, just like methods and variables. This means a public member class acts similarly to a nested top-level class. The primary difference between member classes and nested top-level classes is that member classes have access to the specific instance of the enclosing class.
Local classes
Local classes are like local variables, specific to a block of code.Their visibility is only within the block of their declaration. In order for the class to be useful beyond the declaration block, it would need to implement a more publicly available interface.Because local classes are not members, the modifiers public, protected, private, and static are not usable.
Anonymous classes
Anonymous inner classes extend local inner classes one level further. As anonymous classes have no name, you cannot provide a constructor.
2006-11-27 00:56:05
·
answer #1
·
answered by sushil 2
·
1⤊
0⤋
A class defined within another class is called a nested class. Like other members of a class, a nested class can be declared static or not. A nonstatic nested class is called an inner class. An instance of an inner class can exist only within an instance of its enclosing class and has access to its enclosing class's members even if they are declared private.
2006-11-27 16:27:59
·
answer #2
·
answered by mallikj2♠ 2
·
0⤊
0⤋
For my college honors is extremely like person-friendly classes different than it particularly is extra artwork and it particularly is quicker. You do get so plenty extra artwork than you're able to in someone-friendly type. case in point in my Honors U.S. heritage type we had to make a capacity element, the person-friendly classes in basic terms had to do 5-7 slides however the honors classes had to do 8-13 slides. I additionally think of the teachers excpect so plenty extra out of the Honors scholars using fact via being in the honors type you're proving which you're able to having the flexibility to end the artwork and bypass the class.
2016-12-29 13:27:55
·
answer #3
·
answered by Anonymous
·
0⤊
0⤋
.
Inner Class
http://java.sun.com/docs/books/tutorial/java/javaOO/innerclasses.html
Nested Class
http://java.sun.com/docs/books/tutorial/java/javaOO/nested.html
**********
Vasu M
**********
2006-11-30 23:53:54
·
answer #4
·
answered by V@su Maniram 3
·
0⤊
0⤋