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.
Joined: May 25, 2003
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?
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:
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();