aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Nested Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Nested Class" Watch "Nested Class" New topic
Author

Nested Class

santhoshK babu
Greenhorn

Joined: Jul 25, 2006
Posts: 22
hi everybody,
The following code catches the following error...

Can any one say why it is not possible to create object for an inner class..

class j{
int p=58;
class k{
int l=87;
}
public static void main(String argsssss[]){
j obj1=new j();
k obj2=new k();
}

}

/*Output
F:\san>javac j.java
j.java:8: non-static variable this cannot be referenced from a static context
k obj2=new k();
^
1 error
*/

Thanks in advance..
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
The inner class is a member of the class definition, and cannot be accessed from a static context.

In the main method you need to create an instance of the class first, and then use that to create the instance of the inner class.
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
How to create instances of static nested classes and inner classes?
Ramamoorthy Periasamy
Ranch Hand

Joined: Feb 06, 2006
Posts: 30
try this
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Nested Class