Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

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
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!