• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Some clarification on inner classes pls

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you post the text of the error here??
 
anil bisht
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Lahcen. That makes sense and it cleared my doubt.
MKBala...
 
reply
    Bookmark Topic Watch Topic
  • New Topic