my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inner classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inner classes" Watch "Inner classes" New topic
Author

Inner classes

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
hi Inner classes are defined as having 4 types.
could anyone explain the difference between the 4 below
1. Anonymous Classes,
2. Top Level Nested Classes
3. Member Inner Classes
4. Local Inner Classes
i found this here http://www.geocities.com/swaminr/JavaStudyNotes.htm
Rahul
Vijaya Kumari
Greenhorn

Joined: May 29, 2000
Posts: 1
Hi Rahul,
here are the differences in short--
1. Anonymus classes
are the ones that are instantiated and declared at the
same place. This takes the form :
new XXXX () { //class body}
where XXXX is the interface name it implements.
2. Top Level Nested Classes
These are the static inner classes. These do not have a hidden reference to the enclosing class reference. these can access only the static member variables of the enclosing class.
3. Member Inner Classes
The inner classes that are declared inside a method of the
enclosing class. These classes have additional access to the final variables of the enclosing method.
4. Local Inner Classes
A simple nested classes, it has access to all member variables of the enclosing class.

Hope this helps..
Vijaya
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
rahul,
I am in a middle of another work. So I just wrote an example for you.
Please don't mind. I have given some comments in bet. Feel free to ask if you want anything at all.
The alignment is not proper. So cut and paste to a Java prog and analyze.
regds
maha anna
Vijaya,
Your defn. for type 1,3,4 have to be changed I think.
Your defn1 : anonymous classes can also extend a non-final class
Your defn 3 and 4 have to be interchanged. Local inner classes are ones which are inside a code block/inside a method and member inner classes are the ones which are inside any class and ouside of any method/code block.

regds
maha anna
<pre>

import java.awt.event.*;
class Test {
static {
// Type 4. Local inner class inside a block of static floating block
class LocalInnerClassInsideABlock {
}
}

{
//Type 4. Local inner class inside a block of instance floating block
class LocalInnerClassInsideABlock {
}
}
/* Type 3.annonymous innerclass implementing an interface and in the declaration of a member var. They can also be inside a code block or a method*/
ActionListener al = new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Button clicked");
}
};
// Type 3. annonymous innerclass extending a class
Test tst = new Test() {
public void sayHelloWorld() {
System.out.println("Hello world");
}
};

// Type 1. Top level nested inner class
static class TopNestedInnerClass {
}

// Type 2. Member inner class
class MemberInnerClass {
}
public static void main(String[] args) {

//Type 3. anonymous inner class inside a method here it happend to be Local class inside this main(..) method
ActionListener al = new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Button clicked");
}
};

// Type 4. Local Inner class inside a METHOD
class LocalInnerClass {
}
}
}


[This message has been edited by maha anna (edited May 31, 2000).]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
hi maha Anna,
it looks like the difference between Member Inner class and Top level nested class is that top level nested classes are static while member classes are not. also IMO are member classes and nested classes refer to the same thing. is this true?

// Type 1. Top level nested inner class
static class TopNestedInnerClass {}
// Type 2. Member inner class
class MemberInnerClass {}
thanks for u r reply
Rahul
Greg Belyea
Greenhorn

Joined: Feb 06, 2004
Posts: 29
No Top Level nested classes are not necessarily static, he just put in a static block of code, this does not refer to a class just a body of executable code
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
rahul_mkar,
Yes. You are right. Nested Top Level inner class is the one, which has static keyword as you said in Type 1.
(R&H also refer this as static inner classes )
Member inner classes are the ones WITHOUT the static keyword which exists only when an instance of outer class exists. In other words in order to create an instance of 'member inner class '(the one without the static keyword and inside a class defn as you said in Type 2), you MUST have an instance of the enclosing outer class.

gbelyea,
What rahul_mkar interprets is correct. He is referring to static inner class also called as TopLevel/nested inner class. But if I am not wrong you seem to refer to the inner class which is inside the static floating block . Why is that ? Can you be little bit more elaborate on your interpretation. Any class defined inside a method/block of code (static/instance floating types) are called local inner classes . If I am not wrong you seem to mix the 2 types.
regds
maha anna
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inner classes