• 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

Inner Class

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please see the following code :
public class Outer
{
private int x;
public class Inner
{
private int y;
public void innermethod()
{
System.out.println("This is inner X : " +x);
System.out.println("This is inner Y : " +y);
}
}
public void outermethod()
{
System.out.println("This is outer X : " +x);
}
public static void main(String args[])
{
Inner in = new Inner();//gives a compile error
in.innermethod();
}
}
This gives a compile error saying non-static variable this cannot be referenced from a static method WHY?. and How can this be solved ?.
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To instantiate an inner class, you need an instance of the outer class. That means that you have to write the class creation expression as follows
Outer.Inner in = new Outer().new Inner();
and it will work fine.
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Outer
{
private int x;
public class Inner
{
private int y;
public void innermethod()
{
System.out.println("This is inner X : " +x);
System.out.println("This is inner Y : " +y);
}
}
public void outermethod()
{
System.out.println("This is outer X : " +x);
}
public static void main(String args[])
{
Inner in = new Outer().new Inner();
in.innermethod();
//Inner in = new Inner();//gives a compile error
//in.innermethod();
}
}
 
Rajeev Nair
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Val
 
Joshua Kueck
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wouldn't this also work?
public void printAnswers()
{
Inner in = new Inner();//gives a compile error
in.innermethod();
}
public static void main(String args[])
{
Outer out = new Outer();
out.printAnswers();
}
Im guessing it can get to "this" since it is not in the static method anymore. Correct me if Im wrong, just started doing Java a couple weeks ago.
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you try to compile and run it you would see that it works
The reason is because in printAnswers() there is an implicit reference to this (referring to the actual instance of Outer), and thus, the class creation works.
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Valentine,
I'd just like to validate this:
We have to use Outer.Inner in = new Outer().new Inner() or Outer out = new Outer(); Outer.Inner in = out.new Inner(); in the main method because it is static but in a non-static method we can just use Inner in = new Inner() because of the keyword "this" which automatically associates an instance of the outer class to the inner class instance we've just created.
If Inner class is static then we need not associate an instance of the outer class in the main method like this Inner in = new Outer.Inner(); because the main method is also static.
Thank you
Tina
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's right Tina,
but the problem at hand was about inner class and not nested class. Maybe I should have added what you just wrote, that way, my explanation would have been more complete.
BUT (there is always a 'BUT') if you change the declaration of Inner to be static, you won't be able to access the member x from within innermethod() unless you declare it to be static too. Just for completeness.
[ March 14, 2002: Message edited by: Valentin Crettaz ]
 
Tina Ang
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Valentin! What's the difference between an inner class and a nested class?
Tina
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


in the main method like this Inner in = new Outer.Inner(); because the main method is also static.


thats only right when your main is inside Outer
otherwise a qualified reference should be used.
and nested :static inner class(also called top-level nested inner class)
while inner class is not static.
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
aymen
and nested :static inner class(also called top-level nested inner class)
while inner class is not static.

You are right about the fact that a nested class is a static inner class. But we don't call them top-level nested inner class. Because the word "nested" already implies an enclosing class, and thus, a nested class is always to be found within the declaration of another.
Bottom line is:
- a top-level nested class (aka static inner class) has no reference to an instance of the enclosing class (exactly like static methods!!)
- an inner class has a reference to an instance of the enclosing class.
Here is what JLS 8 Classes says about this terminology issue:


A nested class is any class whose declaration occurs within the body of another class or interface. A top level class is a class that is not a nested class.
...
Member class declarations (�8.5) describe nested classes that are members of the surrounding class. Member classes may be static, in which case they have no access to the instance variables of the surrounding class; or they may be inner classes (�8.1.2).
...


JLS 8.5 Member Type Declarations


A member class is a class whose declaration is directly enclosed in another class or interface declaration.
...
If a member class or interface declared with simple name C is directly enclosed within the declaration of a class with fully qualified name N, then the member class or interface has the fully qualified name N.C.


JLS 8.5.2 Static Member Type Declarations


The static keyword may modify the declaration of a member type C within the body of a non-inner class T. Its effect is to declare that C is not an inner class. Just as a static method of T has no current instance of T in its body, C also has no current instance of T, nor does it have any lexically enclosing instances.
...
It is a compile-time error if a static class contains a usage of a non-static member of an enclosing class.
Member interfaces are always implicitly static. It is permitted but not required for the declaration of a member interface to explicitly list the static modifier.

reply
    Bookmark Topic Watch Topic
  • New Topic