This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
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 REST with Spring (video course) this week in the Spring forum!
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: 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
subject: Nested Class
It's not a secret anymore!