Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Static Inner Class Extending Non Static Inner Class

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
 
Sasparilla and fresh horses for all my men! You will see to it, won't you tiny ad?
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic