help me understand static variables
well here's my problem at work they got a nice book called thinking the 4th
and in that book there's a about static
I'm using IntelliJ IDEA 6.0
and is it true i can't do this
publicclass main
{
publicstaticvoid main(String[] arg)
{
class StaticTest
{
staticint t = 43;
}
}
}
or just out side theclass
publicclass main
{
publicstaticvoid main(String[] arg)
{
staticint t = 43;
}
}
I'm not sure why I get errors on those two codes
but I hope you can help me out :)
Greetings
David
[1437 byte] By [
Entvexa] at [2007-11-27 0:26:35]

Static variables and methods are associated with the class itself, not with separate instances of the class.
A common use case for static variables is to use them to define "constant" or "default" values shared by all instances of the class. For example:public class HttpServer {
public static final int DEFAULT_PORT = 80;
}
A common use case for static methods is to use them a "utilities" that can be access by all instances of the class or even other classes. Those methods can be accessed without creating an instance of the class. For example, the java.util.Math class contains only static methods. It can't even be initialized.
static: associated with a class
member: associated with an instance (object)
local: associated with a method or even smaller block