File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Nested Class" Watch "Nested Class" New topic

Nested Class

santhoshK babu

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();


F:\san>javac 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: 2398
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:
subject: Nested Class
It's not a secret anymore!