I have a static method(addItem) in a class, why do we need to wrap the addItem(..) uses with static parenthesis? why do we need the static word? TNX
public class Something{
static {
addItem(new DummyItem("1", "A"));
addItem(new DummyItem("2", "B"));
addItem(new DummyItem("3", "C"));
}
private static void addItem(DummyItem item) {
......
}
}
The static block
static {
....
}
defines a static initializer. The block of code is run exactly once, when the class is initialized. You can call static methods in any method, not just in static intializers. So for example you could also write
public void foo() {
Something.addItem(new DummyItem("1", "A"));
}
This code would be called whenever the method foo() was called.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.