Overwriting
public class Test {
static int x;
static { x = 1; }
Enum
Method | Description |
---|---|
static E valueOf(String name) | Returns an enumeration constant with the specified name |
static E[] values() | Return an array of enums |
String name() | Return the enumeration constant name as a character string |
int ordinal() | Returns the ordinal of an enumeration constant starting from 0 |
interface A {
static void test1() {
System.out.println("static");
}
default void test2() {
System.out.println("default");
}
}
class B implements A { }
class test {
public static void main(String[] args) {
A.test1(); // 「static」
A.test2(); //Compile error
B b = new B();
b.test1(); //Compile error
b.test2(); // 「default」
}
}
public class Singleton {
private static Singleton s = new Singleton(); // 1
private Singleton() {} // 2
public static Singleton getInstance(){ // 3
return singleton;
}
}
(1) Declare a private static field so that only one instance is always created. (2) Declare the constructor as private so that an instance cannot be created from the outside. (3) Declare a public static method that returns an instance so that the instance reference can be obtained from the outside.
Recommended Posts