File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How to Contruct an Inner Class Instance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How to Contruct an Inner Class Instance" Watch "How to Contruct an Inner Class Instance" New topic
Author

How to Contruct an Inner Class Instance

Richard Wilson
Ranch Hand

Joined: Jan 12, 2002
Posts: 57
According to "Complete Java Certification Guide",
you can create an inner class(member inner class,
not static inner class) like this form:
OuterClass.InnerClass name=new OuterClass().new InnerClass();
But I see somewhere the inner class created like this:
InnerClass name=new OuterClass().new InnerClass();
In this form,the OutClassName is omitted.
Which is right then???


Richard Wilson
Erik Dark
Ranch Hand

Joined: Jan 28, 2002
Posts: 107
Richard,
As far as I've learned (here, thnx Rob) your create a inner class reference like this:

So you still need the reference through the OuterClass.
Erik Dark
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Richard, there is no such thing as a "static inner class." If a nested class is static, it is a nested top-level class.

The way Erik described it will always work.
However, IF you are creating the inner class from inside the outer class, then the inner class is a member of the outer class, and you can omit the name of the outer class.


Rob
SCJP 1.4
Richard Wilson
Ranch Hand

Joined: Jan 12, 2002
Posts: 57
Bob,
What if I create an instance of inner class inside a static method like the following code shows:
public class innerClass{
class inner1{}
public static void main(String[]args){
inner1 tomato1=new innerClass().new inner1();
}
}
//the code compiles without error.
I do not know why since static method has no such relation with the instance of outer class???
[ February 10, 2002: Message edited by: Richard Wilson ]
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Richard,
you're correct that within a static method, there is no associated instance of the enclosing class, so you have to insantiate the outer class to create the inner class, as when you write
new innerClass().new inner1()
However, the static method is still a member of the innerClass class, and so any members defined in innerClass are in scope in that method, so that the static method can still refer to inner1 by its simple name. This is possible because both the static method and the inner1 class are members of the innerClass, the enclosing class.
Bob Graffagnino
Ranch Hand

Joined: May 30, 2001
Posts: 81
Originally posted by Rob Ross:
Richard, there is no such thing as a "static inner class."

Could you explain the difference between an inner class vs a nested class? I always considered them to be the same. Since there are nested classes that are static, I considered static inner classes to be valid.
Perhaps the distinction is that an inner class is a classes' definition within a class and a nested class is the instantiation of that class within a class. ???
Bob Graffagnino
Ranch Hand

Joined: May 30, 2001
Posts: 81
After doing some reading, I've concluded that static inner classes do exist.
Roberts, Heller, & Ernest defines a static inner class as "an inner class that is associated with the class rather than any particular instance of that class" ("Complete Java 2 Certification", RHE, P.192).
Mughal & Rasmussen define a top-level nested class as "a static member in a top level class" ("A Programmer's Guide to Java Certification, M&R, p.224).

Is there a difference between:

and
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Bob,
your second code has nothing to do with inner classes. You just have a static reference variable "ac" of class AnotherClass. AnotherClass is also a top-level class declared at the same level as TopLevelClass.


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Bob Graffagnino
Ranch Hand

Joined: May 30, 2001
Posts: 81
Originally posted by Valentin Crettaz:
[QB]Bob,
your second code has nothing to do with inner classes

I agree that the second example does not have anything to do with an inner class. But doesn't the keyword static have the same effect in both examples?
I'm getting confused with M&R's definition of a top-level nested class:
"A static member in a top-level (possibly nested) class or interface."

According to the definition, aren't both of my examples top-level nested classes?
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Nope, in the second example you just declare a static member in TopLevelClass just the same way you would declare static integer member:

Note that the syntactic difference lies in the use of the keyword "class". In the first example, you write
static class AnotherClass
within the declaration of class TopLevelClass, while in the second example you don't use the keyword "class" at all, so you just declare a static member.
HIH
Bob Graffagnino
Ranch Hand

Joined: May 30, 2001
Posts: 81
Okay, it's taking me a while to articulate the puzzle in my head.
Let examine 3 examples:
Example 1:

Example 2:

Example 3:

What is the difference between declaring an inner class declaration static vs declaring the instance of an inner class static?
[ February 11, 2002: Message edited by: Bob Graffagnino ]
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
static can be applied to classes as well as to members.
1. Inner is not static but the reference i to an instance of it is.
2. Inner is static but the reference i to an instance of it is not.
3. Both Inner and a reference i to it are static.
You have to make the difference between declaring a class "static" and an instance of it "static", this is not the same.
1. Inner is not static and thus requires an instance of TestClass to be created. The reference i of type Inner is static, therefore there will be only one object of type Inner for all instances of TestClass because i is a static member of class TestClass.
2. Inner is static and thus does not require an instance of TestClass to be created. The reference i of type Inner is not static and thus each instance of TestClass will have its own reference i to a different object of type Inner.
3. Both Inner and i are static, that means that we don't need any instance of TestClass to create an object of type Inner. Moreover, the reference i will be the same for all instances of TestClass since it is declared static.
HIH
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Richard Wilson:
According to "Complete Java Certification Guide",
you can create an inner class(member inner class,
not static inner class) like this form:
OuterClass.InnerClass name=new OuterClass().new InnerClass();
But I see somewhere the inner class created like this:
InnerClass name=new OuterClass().new InnerClass();
In this form,the OutClassName is omitted.
Which is right then???

This may seem like a big trip to the past, but this thread seems to have digressed a bit and I've found a little more to add to the original post.
It is possible to use import statements so that you do not have to name inner classes by their fully qualified names ( i.e. <outer_class>.<inner_class> ).
Let's assume you have the following class (and inner class):

Then, you can use import statements to access the inner class (even from a different package) without using the fully qualified name.

In the example above, line 1 is always legal, regardless of the import statements. Line 2 is legal because of import statement number 1. Line 3 is legal because of import statement number 2. So, you see, Richard, either way is fine, depending upon your import statements.
Corey
[ February 11, 2002: Message edited by: Corey McGlone ]

SCJP Tipline, etc.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
> .
Hmmm...after noticing a typo in my last post, I tried to go back to fix it only to find that I didn't really make a typo. I used the following character combination:
'>' ')' '.' and I ended up with this:
I tried it again (that's the smiley you see at the top), and found that the combination of those three characters indeed results in the ')' being turned into a smiley face.
Is this a known issue? If not, I can post it in the JavaRanch forum.
In short, please don't think I'm an idiot for putting that smiley in where it didn't belong. It wasn't my fault, really! The computer ate my parenthesis!
Actually, after thinking about it for a good 4 seconds, I think I realized what caused it. > is converted to &gt; and the following ')' caused the simley face. My fault - sorry, all.
There, I got it fixed - I just added some whitespace. Let them be a lesson to all of you - NO SPECIAL CHARACTERS PRIOR TO A PARENTHESIS!
Corey
[ February 11, 2002: Message edited by: Corey McGlone ]
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Another inner class resource:
http://www.dcs.ed.ac.uk/teaching/cs3/ipcs/APJ/apj9.pdf
Bob Graffagnino
Ranch Hand

Joined: May 30, 2001
Posts: 81
Okay, I think I got it. To summarize:
A inner class definition declared static means that an instance of the outer class is not needed to instantiate an instance of the inner class.


A static reference (instance) of a class that is a member of a surrounding class means that there is only one instance of the member class for all instances of the surrounding class.

Do I have this staight?
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
You got it Bob
 
Consider Paul's rocket mass heater.
 
subject: How to Contruct an Inner Class Instance
 
Similar Threads
Q: Anonymous inner class extending outer class
Nested & Inner class
ExamLab Question-2
Nested Classes
inner classes