HomeSearch

Java static Keyword

Use static fields and static methods. A static member is tied to a type, not an instance of the type.

Static. In stasis, a thing exists, but in only one place, at one time. It is singular. A static variable is tied to no class instance. It occupies one location in memory.

Similarly, static methods act on no instance data. They operate on static data. They are often entry points. But we can create (or access) instances of classes inside them.Class

Field, method. This program has 2 static things. It has a static field, named count, that is of type int. Second it has the main() entry point, which is where control flow begins.

Tip: Main can access the count field because the field is static. It uses the static keyword.

And: A static field can be accessed by both static and instance methods. An instance field, however, cannot be accessed by static methods.

Java program that uses static variable public class Program { static int count; public static void main(String[] args) { // Increment static variable. Program.count++; System.out.println(Program.count); // Repeat the increment. // ... The memory was not erased. Program.count++; System.out.println(Program.count); } } Output 1 2

Instances versus static. Consider this program. It has a static int and a non-static int in the Program class. We access the static "size" with Program.size.

But: To access the instance field "code," we must first create an instance of the Program class. It is tied to an instance, not the type.

Java program that uses instances, static public class Program { static int size = 100; int code = 1000; public static void main(String[] args) { // Access static field with type name. System.out.println(Program.size); // Access instance field with instance. Program p = new Program(); System.out.println(p.code); } } Output 100 1000

Final versus static. A final field cannot be changed in a program. A static field, existing in only one place, can be changed. If you try to change a final field, a compiler error occurs.Final

Final: This means "constant." A local variable may be declared final: it is a constant local.

Static: This means "in one place." Locals may not be static—we can use static fields and methods.

Java program that uses final, static public class Program { static int _id = 5; final int _code = 100; public static void main(String[] args) { _id++; // This is valid. _code++; // This fails. } } Output Exception in thread "main" java.lang.Error: Unresolved compilation problem: Cannot make a static reference to the non-static field _code

Benchmark, static method. Compilers implement instance methods with an invisible first argument: the instance itself. I timed an instance method (getNumber) and a static one (getNumber2).

Version 1: This version of the code calls an instance method in a nested loop. The method is called many times.

Version 2: Here we call a static method the same number of times in a nested loop.

Result: The static method calls were faster. They do not require the instance, so are simpler and faster.

Java program that times static method public class Program { int getNumber(int a) { // An instance method. return a * 2; } static int getNumber2(int a) { // A static method. return a * 2; } public static void main(String[] args) throws Exception { Program p = new Program(); long t1 = System.currentTimeMillis(); // Version 1: call instance method. for (int x = 0; x < 100; x++) { for (int i = 0; i < 10000000; i++) { if (p.getNumber(i) < 0) { throw new Exception(); } } } long t2 = System.currentTimeMillis(); // Version 2: call static method. for (int x = 0; x < 100; x++) { for (int i = 0; i < 10000000; i++) { if (Program.getNumber2(i) < 0) { throw new Exception(); } } } long t3 = System.currentTimeMillis(); // ... Times. System.out.println(t2 - t1); System.out.println(t3 - t2); } } Output 955 ms, int getNumber() [Instance] 880 ms, static int getNumber2() [Static]

Benchmark, static fields. In my test, a static field is slower to access than an instance (class-based) field. The time increase is consistently measurable.

Version 1: This version of the code access an instance field on the Program class many times.

Version 2: Here we access a static field on the Program class—we do not use a Program reference to access the field.

Result: It is slower to access a static field. Using instance-based fields is faster.

Java program that benchmarks static fields public class Program { int field1 = 100; static int field2 = 100; public static void main(String[] args) throws Exception { Program p = new Program(); long t1 = System.currentTimeMillis(); // Version 1: use instance field. for (int x = 0; x < 100; x++) { for (int i = 0; i < 10000000; i++) { if (p.field1 * 2 <= 199) { throw new Exception(); } } } long t2 = System.currentTimeMillis(); // Version 2: use static field. for (int x = 0; x < 100; x++) { for (int i = 0; i < 10000000; i++) { if (Program.field2 * 2 <= 199) { throw new Exception(); } } } long t3 = System.currentTimeMillis(); // ... Times. System.out.println(t2 - t1); System.out.println(t3 - t2); } } Output 763 ms, field1 [Instance field] 953 ms, field2 [Static field]

Instance arguments. To a compiler, instance methods are often static ones with a hidden first argument—the instance reference. Static methods can receive class arguments.

So: The key difference between a static and instance method is clarity of syntax. It is easier to decide what an instance method does.

Thus: Object-oriented programming is about clarity and maintainability. It makes complex programs easier for regular people to build.

A balance. We achieve speed increases with static methods. But if those methods use excessive static fields, we lose our optimization. A delicate balance is needed for optimal speed.

A review. Static methods and fields are used throughout Java programs. The main() method, used in every program, is itself a static method.
Home
Dot Net Perls
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.