• 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

Static Inner Class Extending Non Static Inner Class

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Source :Self

Hi,
When implementing a static inner class, i tried to extend itselt from a Non-static inner class.

Code is as follows
public class Serialization {

public static void main(String...strings) throws Exception
{
Serialization serialization = new Serialization();
InnerSubStatic1 inner = new Serialization.InnerSubStatic1();
inner.j = 100;
inner.i = 200;
System.out.println(" i value is " + inner.i);
System.out.println(" j value is " + inner.j);
}
class SuperInnerNonStatic
{
int j = 5;
}
static class InnerSubStatic1 extends SuperInnerNonStatic
{
int i = 10;
InnerSubStatic1()
{
super(); //line1

}
}
}

I got exception as "No enclosing instance of type Serializable is available due to some intermediate constructor invocation" at line1
TO my surprise i replace //line1 with "new Serialization().super();". It worked . Is there any other way?

thanks
rami
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static members don't have access to super() or this() calls. In order to make a call to super or this there must be an instance of the class, and since static method don't depend on instantes of the class, then there's no point in calling those methods. See that it work when you created an instance with new?

BTW, I think that when you called new Serialization().super(), the super is a call to the Object constructor, which doesn�t do what you wanted.
 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not able to get this.
Serialization is a non static class.
Please explain me in depth with simple terms
 
Ram Reddy
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi daniel,

If i am not wrong ,to create instance for the static class we can have the constructors for the static class same as Non-static class.

Here is the entire code , contains calls to this() and super() in Static class.

public class Serialization {

public static void main(String...strings) throws Exception
{
InnerSubStatic inner = new Serialization.InnerSubStatic();
inner.j = 100;
inner.i = 200;
System.out.println(" In Main i value is " + inner.i);
System.out.println(" In Main j value is " + inner.j);
}
class SuperInnerNonStatic
{
int j = 5;
SuperInnerNonStatic(int i)
{
this();
System.out.println("Here in Inner Static class I value is " + i);
}
SuperInnerNonStatic()
{
super();
}
}
static class InnerSubStatic extends SuperInnerNonStatic
{
int i = 10;
InnerSubStatic()
{
new Serialization().super(12);
//No enclosing instance of type Serializable is available due to some intermediate constructor invocation
}
InnerSubStatic(int k)
{
this();
}
}
}
Is this what you are trying to expalin?
Can you elaborate it if this is not ?

thanks
rami
 
I’m tired of walking, and will rest for a minute and grow some wheels. This is the promise of this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic