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?
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.
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