aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Some clarification on inner classes pls Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Some clarification on inner classes pls" Watch "Some clarification on inner classes pls" New topic
Author

Some clarification on inner classes pls

Balamurugan Kandasamy
Greenhorn

Joined: Dec 03, 2000
Posts: 19
The following compiles.
import java.lang.*;
public class A {
static class B {}
}
class C extends A.B {
public static void main(String args[])
{
A.B ab = new A().new B();
}
}
But when I remove static for class B which is as below , it says
no enclosing instance of type A in scope.
import java.lang.*;
public class A {
class B {} // note than static is removed.
}
class C extends A.B {
public static void main(String args[])
{
A.B ab = new A().new B();
}
}

However if i dont extend A.B then it is not a problem.
import java.lang.*;
public class A {
static class B {}
}
class D {
public static void main(String args[])
{
A.B ab = new A().new B();
}
}
Think im not clear in this funda. Pls throw some light.
Thanks
MKBala...
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
A non-static inner class is always associated with an instance of an enclosing class. ie., if B is a non-static inner class of A, then you will have to instantiate A in order to instantiate B.
Since the main method is in a static context, you can instantiate a static inner class from within the main method, but not a non-static inner class. That is the reason why you get a compiler error soon after removing the static qualifer for the inner class.
Hope that helps!
Ajith


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
anil bisht
Ranch Hand

Joined: Nov 04, 2000
Posts: 81
one more thing..
i read that to create the object of static class
A.B ab = new A.B()
and for nonstatic class its
A.B ab = new A().new B()
its ok i am convinced..
its also okay to create an objext of new class using
A.B ab = new A().new B()
this dosent give the compile time error but gives teh run time error ?? why ???
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
Can you post the text of the error here??
anil bisht
Ranch Hand

Joined: Nov 04, 2000
Posts: 81
class C {
static class B {
}
}
class A extends C {
public static void main(String args[])
{
C.B ab = new C().new B(); // gives runtime error
C.B ab = new C.B(); // works fine
}
}
the error is
java.lang.VerifyError: (class: A, method: main signature: ([Ljava/lang/String V) Expecting to find unitialized object on stack
Exception in thread "main" Normal Termination
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
Anil,
This is a bug in the compiler. Apparently whenever you use the first version( new Outer().new Inner()) while instantiating the static Inner, the Outer() instance gets messedup. Take a look at this bug at Sun's bug parade and also checkout all the related bugs that are displayed a part of this one.
The suggested workaround is to use the second version!
Ajith
Balamurugan Kandasamy
Greenhorn

Joined: Dec 03, 2000
Posts: 19
Hi Ajith,
I think I am not comfortable yet. The point that you need an instance of outer class for instantiating the inner class is fine. So in that case my code below should compile fine right ???
import java.lang.*;
public class A {
class B {} // note than static is removed.
}
class C extends A.B {
public static void main(String args[])
{
A a = new A(); // instantiated the outer class
A.B ab = a.new B(); // instantiating the inner class through
// outer instance.
}
}
But my doubt is ,if my problem is due to inheritance, bcos for the same above code when i say extends A instead of A.B it is fine.
So does that mean that the inner non-static class B cannot be inherited without the whole class A being extended.
Thanks
MKBala...
Lahcen Mannou
Greenhorn

Joined: Oct 09, 2000
Posts: 7
Here is (I think ...) the explanation of your problem :
try this code, you will not get a compilation error any more :
public class A {
class B {} // note than static is removed.
}
class C extends A.B {
// Added code BEGIN
C(A a) {
a.super();
}
// Added code END
public static void main(String args[])
{
A.B ab = new A().new B();
}
}
The problem is that the compiler tries to provide a default Constructor to your class C. The default constructor looks like that :
C() {
super();
}
But this default constructor will not works, because in order to get an instance of A.B, you have first to get an instance of A.
You must then provide an instance of A.
Lahcen.
Originally posted by Balamurugan Kandasamy:
The following compiles.
import java.lang.*;
public class A {
[b]static
class B {}
}
class C extends A.B {
public static void main(String args[])
{
A.B ab = new A().new B();
}
}
But when I remove static for class B which is as below , it says
no enclosing instance of type A in scope.
import java.lang.*;

However if i dont extend A.B then it is not a problem.public class A {
class B {} // note than static is removed.
}
class C extends A.B {
public static void main(String args[])
{
A.B ab = new A().new B();
}
}
import java.lang.*;
public class A {
static class B {}
}
class D {
public static void main(String args[])
{
A.B ab = new A().new B();
}
}
Think im not clear in this funda. Pls throw some light.
Thanks
MKBala...[/B]

Balamurugan Kandasamy
Greenhorn

Joined: Dec 03, 2000
Posts: 19
Thanks Lahcen. That makes sense and it cleared my doubt.
MKBala...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Some clarification on inner classes pls