• 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

Intermediate constructor invocation

 
Ranch Hand
Posts: 68
MyEclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a self written code.


ERROR says:no enclosing instance of type Practice1 is available due to some intermediate constructor invocation

i am not getting why this error is coming..please help me..

Sumit
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. You must be running the code in Eclipse, the error message on the console is different.
2. As you might know, you need an instance of the enclosing class if you want to create an instance or extend an inner class. So to extend class B, class C needs an instance of Practice1 class. Class C itself has an instance of class Practice1 but you can't use that instance to call the super constructor.

Solution, use a statement like new Practice1().super();...
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting, so does that mean inner classes don't have access to the members of it's outerclass?
 
Ranch Hand
Posts: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can we relate this problem with Multiple Inheritance? I think it has some relation with that...but not very sure..need help to fix my
doubt, please help..
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:Solution, use a statement like new Practice1().super();...


I don't like that...since you're calling the B constructor within an unexpected object.

The following appears to work, though:
Rationale: an instance of C is always created within an A. So that's the instance of A you want to call the B constructor with. And Practice.this is how you'd get a reference to it. And that seems to do the trick.

(And no, this is nothing to do with multiple inheritance ).
 
Sumit Khurana
Ranch Hand
Posts: 68
MyEclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

You must be running the code in Eclipse, the error message on the console is different.


Yes Ankit,you are right..

Solution, use a statement like new Practice1().super();...


I think,mathew is right.why do we create a separate object for calling the super().because we already have the object of Practice1.

But i am still in doubt.

calling a Practice1.this.super() or new Practice1().super()(an extra call to constructor)both are going through same route.
first calling the B constructor and than Practice1.

but i am not able to understand these two codes clearly..

I am telling you what i am thinking about this..

before calling the super() we have three objects
1 of B
1 of C
1 of Practice1
objects of B and C are associated with object of Practice1
C also extends B.so,when we call the super() it should call the constructor of its supertype but there is an error.


if we use Practice1.this.super() //here the confusion comes into play
we have a single object of Practice1 but we are connected to it through the ways:
1. through association of C
2. through class B which it extends

Practice1.this.super() refers to Practice1 object.so,it should call Practice1 constructor but it is going through class B constructor.why does it choose that way??

this.super() refers to which one??



 
reply
    Bookmark Topic Watch Topic
  • New Topic