0%

JavaSE static

Where to use key word static?

  • method
  • field
1
2
3
4
5
6
7
8
9
10
11
12
public class Koala{
public static int count = 0; // static variable
public static void main(String[] args){ // static method
System.out.println(count);
}
}

public class KoalaTester{
public static void main(String[] args){
Koala.main(new String[0]);
}
}

Why we need it?

  • for utility or helper methods that don’t require any object state
  • for state that is shared by all instances of a class, like a counter

How to call a static variable or method?

1
2
3
4
Koala k = new Koala();
System.out.println(k.count);
k = null;
System.out.println(k.count);
  • by classname, like Koala.count
  • by instance, like k.count, although k is null, k.count still works

Static Methods (vs. Instance)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class Static {
private String name = "Static class";

public static void first(){

}

public static void second(){

}

public void third(){
System.out.println(name);
}

public static void main(String[] args){
first();
second();
third(); // cannot compile
}
}

third() is a instance method, so it cannot be inferred to static. There are two ways to modify,

  • add static to third() method, but name should changed to static as well

  • call in this way new Static().third()

Remember two rules,

  • an instance method can call a static method
  • a static method cannot call an instance method

Easy to understand, assume a non-static variable is inside a static method, when static method and variables are loaded into static area at the begining of the program, the non-static stuffs cannot be initialized at that time because they are determined by the runtime.

Static Variables

  • Some static variales are meant to change as the program runs

    1
    2
    3
    public class Counter {
    private static int counter = 0;
    }
  • Some static variales are meant to never change over time

    1
    2
    3
    4
    private static final int NUM_BUCKETS = 45;
    public static void main(String[] args){
    NUM_BUCKETS = 5; // DOES NOT COMPILE
    }
1
2
3
4
5
6
7
8
private static final ArrayList<String> values = new ArrayList<>();
public static void main(String[] args){
values.add("changed"); // it works
}
/**
this list 'values' can add more elements
as long as its reference is not changed
*/

Static Initialization(静态块)

1
2
3
4
5
6
private static final int NUM_SECONDS_PER_HOUR;
static{
int numSecondsPerMinute = 60;
int numMinutesPerHOur = 60;
NUM_SECONDS_PER_HOUR = numSecondsPerMinute * numMinutesPerHOur;
}

It is just like unnamed methods.

There is an special example we should notice.

1
2
3
4
5
6
7
8
9
10
private static int one;
private static final int two;
private static final int three = 3;
private static final int four; // cannot compile
static {
one = 1;
two = 2;
three = 3; // cannot compile
two = 4; // connot compile
}

The static variables with final must be initialized in the static initialization or at the declaration moment. Once initialized, they cannot be changed.

Static import

Please refer to javase import
Also, static imports are for importing static members of classes.

1
2
3
4
5
6
7
import java.util.List;
import static java.util.Arrays.asList;
public class StaticImports {
public static void main(String[] args){
List<String> list = asList("one", "two"); // no Arrays
}
}

Reference