• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static Inner Class Questions

 
Gary Farms
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic