static : Java Glossary

static
static variables and methods might better have been called perClass variables and methods. They inherited this misleading terminology from C++. They are the opposite of instance variables and methods that work on a particular object.

There is nothing static (unchanging) about them. They don’t cling. They are perfectly clear, unlike radio signals garbled by static.

They are allocated when the class is loaded. static refers to a method or variable that is not attached to a particular object, but rather to the class as a whole.

static final when applied to a variable is Javanese for constant. All static methods are automatically final. It is not strictly speaking an error to mark them final, but it is redundant and considered bad form.

static methods work without any this object. static methods are limited to calling other static methods in the class and to using only static variables. They can call instance methods only if they use their own object references — not rely on this..

static methods and variable are in a sense inherited, but not in the same strong sense that instance variables and methods are. You can refer to Dog.bark() as Dalmatian.bark() if no one has written a Dalmatian.bark(). However, if you use Dog.bark() you always get the Dog version and if you say Dalmatian.bark() you always get the Dalmatian version.

Newbies tend to overuse static variables. Consider what would happen if your code were used by several threads simulaneously. With shared static variable they would trip over each other. With local and instance variables they often would not, even without any special sychronisation. Sometimes, of course, you do need the globalness of static variables, but don’t use it where it would make more sense to create a object to track each chain of calculation.

Static typing

The word static is used in a second context, the opposite of dynamic or runtime type. This refers to the compile-time declared type of a variable, compared with the run time actual type it points to. e.g. a Dog variable may point to a Dalmatian object, but not vice versa. The static type (the type of the reference) is Dog and the dynamic type (the type of the object) is Dalmatian. You will often hear Java referred to a language with static type checking. The types of all references are checked for consistency at compile time.

Static Loading

The word static is used in a third context. It refers to ordinary classes explicitly mentioned in the code. Dynamic classes are loaded on-the-fly using Class.forName where the class name is constructed as a String.

Static Web Pages

The word static is used in a fourth context. It refers to web pages the server sends out unchanged. Dynamic pages are modified or even composed from scratch before sending.

Static Nested Classes

Finally, Java has an obscure feature where you can also declare nested classes static. This does not mean all their methods are static, or that they cannot be instantiated, rather it means you instantiate the inner classes independently of the main enclosing class. There is no associated outer class object. Such classes are often called nested static classes. Non-static inner class objects always have an associated outer class object.

Avoiding Static

When you first learn Java, there is a tendency to overused static because it simpler and faster. However, often this choice is not good in the long run. Here are two situation to ovoid static.

static init and threads gotcha

I have discovered a Java gotcha. I don’t recall ever reading about it, though it is documented in the JLS (Java Language Specification). It make perfect sense though it completely baffled me for hours.

Every time I executed a static method in one class, no matter how trivial, synchronized or not, it would just hang. IntelliJ trace would just go to sleep.

I was doing threading, and I had some locks internal to two ConcurrentHashMaps in the class, but they should lock on the Collection, not the whole class.

I turned on -verbose mode and discovered some system locking code was being loaded just prior to the freeze.

I think what happens is this. static init code does a behind-the-scenes lock of the class object. You thus cannot call any methods of the class on a separate thread until the static init code is complete. My static init code was spawning threads which tried to call the class’s static methods.

The rule of thumb is, don’t use static init so fire up complicated code, just use it to init a few fields.

Learning More



available on the web at:

http://mindprod.com/jgloss/static.html
ClustrMaps is down

optional Replicator mirror
of mindprod.com
on local hard disk J:

J:\mindprod\jgloss\static.html
logo
Please the feedback from other visitors, or your own feedback about the site.
Contact Roedy.
Blog
IP:[65.110.21.43]
Your face IP:[54.243.13.30]
You are visitor number 296,087.