This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Non static methods of static inner class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Non static methods of static inner class" Watch "Non static methods of static inner class" New topic
Author

Non static methods of static inner class

Gayathri Ramanujam
Greenhorn

Joined: Aug 14, 2006
Posts: 4
class A
{
String y="AA";
public static void main(String[] args)
{
A x= new A();
A.Inner.Test();
}

static private class Inner{
private void Test(){
System.out.println("Test"+y);

}
}
}
Surendra Kumar
Ranch Hand

Joined: Jul 04, 2006
Posts: 235
This class won't compile as static inner class can not have access to instance members.
Gayathri Ramanujam
Greenhorn

Joined: Aug 14, 2006
Posts: 4
if you forget the y then I get this error

A.java:7: non-static method Test() cannot be referenced from a static context
A.Inner.Test();

I was wondering how to access Test()?
Surendra Kumar
Ranch Hand

Joined: Jul 04, 2006
Posts: 235
new A.Inner().Test();
shilpa Reddy
Ranch Hand

Joined: Jul 26, 2006
Posts: 42
This works

class outer
{
String y="AA";
public static void main(String[] args)
{
outer x= new outer();
outer.Inner y = new outer.Inner();
y.Test();
}

static private class Inner{
private void Test(){
System.out.println("Test");

}
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Non static methods of static inner class
 
Similar Threads
mock question
Static Inner Class
explanation needed
Inner Class
Inner class basics. (Marcus Green's mock exam 2)