File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inner Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inner Class" Watch "Inner Class" New topic
Author

Inner Class

Rajeev Nair
Ranch Hand

Joined: Mar 11, 2002
Posts: 51
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 ?.


Raj<br />Sun Certified Java Programmer
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
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.


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Joshua Kueck
Ranch Hand

Joined: Mar 14, 2002
Posts: 71
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

Joined: Mar 11, 2002
Posts: 51
Thanks Val
Joshua Kueck
Ranch Hand

Joined: Mar 14, 2002
Posts: 71
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
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
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.
Tina Ang
Ranch Hand

Joined: Mar 12, 2002
Posts: 58
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
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
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

Joined: Mar 12, 2002
Posts: 58
Thanks Valentin! What's the difference between an inner class and a nested class?
Tina
aymen esawey
Ranch Hand

Joined: Jan 29, 2002
Posts: 61

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.


Aymen Esawey<br />SCJP <img src="smile.gif" border="0"> <br /><a href="http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=32&t=001968" target="_blank" rel="nofollow">how to nuke the SUN </a>
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inner Class
 
Similar Threads
local nested class
Static Inner Class
Accessing Outer Class variable
Inner and outer class instances
doubt in InnerClass