*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in inner classe 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 "Doubt in inner classe" Watch "Doubt in inner classe" New topic
Author

Doubt in inner classe

Leandro Oliveira
Ranch Hand

Joined: Nov 07, 2002
Posts: 298
my doubts are commented within the code:

Could anyone clarify???
thanks!

Edited By Corey McGlone: Broke up long comment line to preserve page integrity.
[ March 19, 2003: Message edited by: Corey McGlone ]
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271

System.out.println((new Integer(2)).super);// why this cause a compile time error???

What the heck is that? What are you trying to do? From that code, you're trying to create a new Integer object and then access the field named 'super' within that object. Not only does an Integer object not have a field named 'super', it can't have a field named 'super'. super is a keyword in Java and therefore, you can't use it as a field name.

new Outer().super();

I'm not entirely sure what it is you're trying to do here, either. The code says that you're trying to create a new object of type Outer and then invoke the super method on it. Again - you run into the same problems as above - there is no and can never be a method named super.
If you're trying to create an instance on the inner class (that's what I assume you're trying to do), you'd do it like this:

The reason for the goofy syntax is because we must first have an instance of the outer class in order for the inner class to exist.
You can find a lot more information about this here.
I hope that helps,
Corey


SCJP Tipline, etc.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
He is inheriting from an Inner class.
I have parsed the classes without the println statement for clarity's sake:

The constructors of the inner classes (*) takes an argument of the holding class type. You can see how the relationship between the inner object and the outer object is stablished: the parameter is assigned to synthetic field thi$0 of type Outer.
When constructing a derived class of Outer.Inner we must call such constructor and thefore provide an argument of type Outer to the super constructor invocation. This is done by "new Outer().super()" . This expression is translated by javap as "Outer. Inner(Outer)" where the Outer parameter is the object created by "new Outer()." in line 1 and 5:


(*) anonymous and local classes are inner but if declared in a static context they do not take an instance of the outer class upon construction.
_________________________________________________
"new Integer(2)).super"
the keyword "super" attempts to access a matching member of the most immediate supertype (and above). Here you did not specify any member, the expression is invalid.


SCJP2. Please Indent your code using UBB Code
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120

/I know there must be an instance of Outer
//that has some relation with this class, but by
//doing new Outer().super, am not I taking the super
//class of outer, since there should be a call Outer.
//Inner constructor???

No you are not because super() calls the inmediate superconstructor. It is "super" the one who looks for "grandtypes" in case "fathertypes" do not declared a matching member. super() fails miserably is not matching superconstructor is declared in the superclass.
Leandro Oliveira
Ranch Hand

Joined: Nov 07, 2002
Posts: 298
Thank you guys!!!
Jose Botella, every local class and anonymous class is considered inner classes??? where can I learn more about javap and java bytecodes?? do you know any tutorial??
why this causes errors:
System.out.println((new Integer(2)).super());
and this compiles:
new Outer().super();
thank you really mutch.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by leandro oliveira:
and this compiles:
new Outer().super();

That compiles? What does your code look like? Let me see that.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
This might help:
From JLS 8.1.2:

Inner classes include local (�14.3), anonymous (�15.9.5) and non-static member classes (�8.5).

From JLS 8.8.5.1:

Qualified superclass constructor invocations begin with a Primary expression. They allow a subclass constructor to explicitly specify the newly created object's immediately enclosing instance with respect to the direct superclass (�8.1.2). This may be necessary when the superclass is an inner class.

It is not possible to use an explicit constructor invocation (this(), super()) outside a constructor.
About bytecodes? look at:
In artima.com you can read some chapters of Inside The Java 2 Virtual Machine by Bill Venners.
Also
The Java Virtual Machine by Allen Holub.
and of course the JVMSpec. from java.sun.com
Leandro Oliveira
Ranch Hand

Joined: Nov 07, 2002
Posts: 298
Corey McGlone, part of the code I copied from jls in 8.8.5.1 (Explicit Constructor Invocations) I did not understand and decided to post my doubt... It is a bit strange but Jose Botella's explanation looks wise!!! Thank you once again!!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Doubt in inner classe
 
Similar Threads
Nested Classes
why a static method can directly reference non-static class???
Loop Problem
Instantiating Inner Classes
Inner Classes Extension