C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
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
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: 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
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]
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]
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.