• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

A class extending inner class  RSS feed

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone tell me why does it give error at the test class constructor in the following prg?
when i compile it says "an enclosing instance that contains outer,inner is required"

pl help

class Outer {
class Inner { }
}

public class test extends Outer.Inner {
test () { }

public static void main(String [] args) {
Outer.Inner o = new Outer().new Inner() ;
test ii = new test() ;
}
}
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neeta,

That error message just means that you need to move the declaration of class test inside of the declaration of class Outer. Of course, that will mean that you must remove the static main method from class test, because a non-static inner class can not contain static method declarations.
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe I should add an example to the above.

Please note that an instance of a non-static inner class must be associated with an instance of the Outer class. The same is not true of static nested classes.

 
neeta mathur
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much Dan
understood the concept now
 
neeta mathur
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry Dan
But i still don't understand why it is not able to invoke the constructor
i tried in the following way
class Outer {
class Inner { }
class test extends Outer.Inner { test () { System.out.println("in constuctor"); } }
public static void main(String [] args) {
Outer o = new Outer();
Outer.Inner i = o.new Inner();
Outer.test t = o.new test() ;
} }

Pl help
 
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I ran this code:

It prints "in constructor" to the console, so it can (and does) call the constructor.

Jules
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neeta,

I agree with Julian; your program works fine.
 
neeta mathur
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sry guys,
there was some conflict at my system path
Thank you all once again
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!