It's not a secret anymore!*
The moose likes Beginning Java and the fly likes Static Inner Class Questions 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 » Java » Beginning Java
Bookmark "Static Inner Class Questions" Watch "Static Inner Class Questions" New topic
Author

Static Inner Class Questions

Gary Farms
Ranch Hand

Joined: Jun 24, 2000
Posts: 57
When I compile the following program I get compile errors stating that StaticInnerClass can't be found. Also, the System.out.println generates a compile error also. Why? Can a static inner class have an argument list? Here's the program:
public class FinalTest
{
private static int staticvar=88;

public static void main(String[] args)
{
FinalTest.StaticInnerClass();
}
public static class StaticInnerClass
{
System.out.println("staticvar = " + staticvar);
}
}
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Try this:

The compiler was complaining about the "FinalTest.StaticInnerClass()" syntax without the new keyword.
The other complaint was having free floating executable code. Only variable declarations should be free floating, anything else should be in blocks or methods. When I put it in a constructor everything worked fine.

[This message has been edited by Cindy Glass (edited October 29, 2001).]


"JavaRanch, where the deer and the Certified play" - David O'Meara
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Gary
A couple of things:
The error about StaticInnerClass can not be found is that the way you have the line written:
FinalTest.StaticInnerClass();
It is as if you're calling a static method of the FinalTest class called StaticInnerClass, if you're trying to create a new instance of the StaticInnerClass class then use this format:
StaticInnerClass sic = new Test.StaticInnerClass();
The other error your getting about the println statement is because the statement is not inside a block so it thinks you're declaring a new method. You need to put it in a block of code to make it work. Check this out:

hope that helps


------------------
Dave
Sun Certified Programmer for the Java� 2 Platform


Dave
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static Inner Class Questions
 
Similar Threads
A question from JQPlus
Why inner classes cant have static memebers
A question about Inner Class
Singleton vs Static
static block and static variable?