This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell 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


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

}
}
}
 
GeeCON Prague 2014
 
subject: Non static methods of static inner class