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 Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inner classes!" Watch "Inner classes!" New topic
Author

Inner classes!

Dave Johnson
Ranch Hand

Joined: May 25, 2003
Posts: 111
Here is some code found in examcram page 90.

First of all I have no disput with any of the code & I am not complaining in any way.
All I want clearing up is the issue of creation of inner classes. In the above example SymAction is a non-static inner class.
What I would have expected to see in a code example like this one as the creation of the non-static inner class using the outer class. For example Applet1.SymAction SA=new Applet1().SymAction();.
I know that it is me who has not quite understood the code. A little help or any links would be appreciated.
Thanks guys, Dave.
Dave Johnson
Ranch Hand

Joined: May 25, 2003
Posts: 111
In addition to what I have said already I would like to say that I suspect the example being an applet is probably why the usual syntax for creation is different. Could I also whether static inner class need an pre existance of the outer class?
Frank Hale
Ranch Hand

Joined: Mar 25, 2000
Posts: 230
As far as your question goes. What I think is happening is that you don't have to explicity instantiate the inner class like this as long as you are doing it within the outer class that the inner class is apart of:

So as long as I am still within the outer class I can declare a new instance of SymAction like:

I personally prefer to add listeners like so:

[ June 21, 2003: Message edited by: Frank Hale ]
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561

All I want clearing up is the issue of creation of inner classes.


or



I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
Dave Johnson
Ranch Hand

Joined: May 25, 2003
Posts: 111
Thanks to the both of you, that makes sense.
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
Originally posted by Dave Johnson:
What I would have expected to see in a code example like this one as the creation of the non-static inner class using the outer class. For example Applet1.SymAction SA=new Applet1().SymAction();.


Applet1.SymAction is an example of a qualified name. In some cases, you need to qualify your variables because ambiguity may arise. But in this code, you don't have to because the compiler can determine its meaning based on the context in which it is used.

new Applet1().SymAction();
Yes, a non-static inner class requires an instance of the enclosing class. So the call to new Applet1() should provide that. But remember that you are inside an instance method, and every instance method will receive a hidden this parameter. This this reference is used by the instance method to access instance members.
So if you have a code like this:

is actually equivalent to this:

So in your program, the call
SymAction SA=new SymAction();
is equivalen to
SymAction SA=this.new SymAction();
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inner classes!
 
Similar Threads
Panel vs Frame
Minimize Button (Swing)
What is the best way to create GUI components- in-line, methods, inner classes or separate classes
What is the access modifier on a regular inner class?
Passing object reference variables