wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes needs explanation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "needs explanation" Watch "needs explanation" New topic
Author

needs explanation

Venkat Ramsimha
Ranch Hand

Joined: Dec 28, 2004
Posts: 127
public class StaticNestedClassImp
{
String name = "ramu";
public static void main(String args[])
{
method();
StaticNestedClassImp.Nested nc = new StaticNestedClassImp.Nested();
nc.method1();
}
public static void method()
{
String sal = "tenthosand";
static class Nested
{
public void method1()
{
System.out.println("in the nested method");
}
}
}
}
WHY THE ABOVE PROGRAM WITH RESPECTIVE TO "STATIC NESTED CLASSES" IS GIVING "COMPILE TIME ERRORS"

can anybody provide the explanation?

thanks,
venkat
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

First, "local" classes (e.g., those defined within a method body) cannot be static. Second, since they are defined within the scope of the method body, local classes are essentially "private" to that block -- just like a local variable.

If you move the nested class definition outside of the method, this code will compile. In fact, if you keep the class definition static, you don't need an instance of the enclosing class. You can simply say...

Nested nc = new Nested();


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: needs explanation
 
Similar Threads
innerclass question
nested class method
more about nested class topics
top level nested interfaces
reflection of static inner classes