Static methods:
static methods/variables do not need instance.
static methods can't use non-static instance variables, nor non-static methods.
You cannot instantiate a class with private constructor.
If you have a class with only static methods, you can marked the constructor private.
Singleton Pattern: You can have a static instance variable of the class and a private constructor.
Static variables:
static variable: value is the same for all instances of the class.
static variables in a class are initialized before any object of that class can be created. When the class is loaded.
static variables in a class are initialized before any static method of the class runs.
static variables are initialized when the class is loaded.
static initializer is a block of code that runs when a class is loaded.
static final int x;
static {
x=4;
}
static final variables are constants. The value of the variable will never change once initialized.
A final variable must be initialized manually.
A final static variable must be assigned a value either at the time it is declared, or in a static initializer.
A final instance variable must be assigned a value either at the time it is declared, or in the constructor.
A final method means you can't override the method.
A final class means you can't extend the class.
Wrapping a primitive.
Boolean, Character, Byte, Short, Integer, Long, Float, Double.
Wrapping a value:
int i = 1;
Integer iWrap = new Integer(i);
Unwrapping a value:
int unWrapp = iWrap.intValue();
i==iWrap; // true
iWrap.equals(i); // true
Autoboxing:
The compiler does the wrap and unwrap automatically on method arguments, return values, boolean expressions, operations on numbers and assignments.
Format:
int one = 23413423;
double two = 42345423.3342;
String s = String.format("The rank is %,d out of %,.2f", one, two);
Dates:
String.format("%tc", new Date());
Sun Nov 29 14:53:21 MST 2004
String.format("%tr", new Date());
04:23:13 PM
Date today = new Date();
String.format("%tA, %<tB %<td", today); (< use the previous argument)
%[argument number][flags(,)][width][.precision] type
For a time-stamp of "now", use Date. But for everything else, use Calendar.
Calendar cal = Calendar.getInstance();
Class Running Order:
public class Order {
static {
System.out.println("Static Super Class");
}
Order() {
System.out.println("Super Class Constructor");
}
}
class OrderTest extends Order{
static{
System.out.println("Static Subclass");
}
OrderTest() {
System.out.println("Subclass Constructor");
}
public static void main(String[] args) {
System.out.println("In main");
OrderTest ot = new OrderTest();
}
}
Output:
Static Super Class
Static Subclass
In main
Super Class Constructor
Subclass Constructor
Static is the first thing of loading a class. Static is called before constructor.
Visibility Level:
Interface:
public (abstract) | 跨包可见 |
no modifier | 包内可见 |
Interface method:
All methods are implicitly public. 接口方法都是public的,都可以跨包访问。不支持super或this。
Modifier | Interface继承 | Interface复写 | Class复写 |
abstract (no modifier) | 可以 | 只可覆盖 | 可以 |
default | 可以 | 可以 | 可以 |
static | 不可以 | 只可覆盖 | 不可以 |
Default method: Concrete method, can call abstract method.
Static method: Concrete method, can call static method.
Interface variable:
All variables are implicitly public, static, and final. Interface variables are constants. 接口内的变量都是常量。
可继承,可覆盖。
package a;
interface InterfaceA {
void abstractMethod();
static void staticMethod() {
System.out.println("InterfaceA static method");
}
default void defaultMethod() {
abstractMethod();
System.out.println("InterfaceA default method");
}
}
InterfaceA只能在包内访问。default 方法内可以调用static方法和abstract方法。
package a;
public interface InterfaceAA extends InterfaceA {
void abstractMethod();
static void staticMethod() {
System.out.println("InterfaceAA static method");
}
// Override
/*default void defaultMethod() {
System.out.println("InterfaceAA default method");
}*/
}
InterfaceAA可以在包外访问。default方法可以在Interface中继承,也可以在Interface中复写。
测试:
package b;
import a.InterfaceAA;
class Test implements InterfaceAA {
@Override
public void abstractMethod() {
System.out.println("Test implemented abstract method");
}
}
public class TestDrive {
public static void main(String[] args) {
InterfaceAA testAA = new Test();
testAA.abstractMethod();
testAA.defaultMethod(); // Call InterfaceA's method, also can be overridden in test.
InterfaceAA.staticMethod();
}
}
在另外一个包中实现InterfaceAA,testAA.defaultMethod()调用的是InterfaceA的default方法。InterfaceA只能在包内访问,但通过继承其default方法可以在包外调用,既方法的访问权限扩大了。Test中也可以复写default方法。
总结:
- 接口的方法和常量的访问权限由接口的访问权限确定,要么包内,要么跨包。
- abstract方法可覆盖(重名,没有body所以算不上复写)。
- default方法可以继承,可以复写。继承可以扩大default方法的访问权限。
- static方法可覆盖,不能被继承,更不能被复写。
- 常量可继承,可覆盖(重名)。
public | 跨包访问 |
no modifier | 包内访问 |
final类不能被继承。
abstract类不能被实例化。类中含有abstract方法必须声明为abstract类。abstract类中可以有concrete方法。
Class Method:
Modifier | 包内对象访问 | 包外对象访问 | 类内访问 | super | 复写 |
public | 可以 | 可以 | 跨包 | 跨包 | 跨包 |
protected | 可以 | 复写后可以 | 跨包 | 跨包 | 跨包 |
package (no modifier) | 可以 | 不可以 | 包内 | 包内 | 包内 |
private | 不可以 | 不可以 | 类内 | 不支持 | 只可覆盖 |
Modifier | 包内类访问 | 包外类访问 | 类内访问 | 父类类名访问 | 复写 |
public static | 可以 | 可以 | 跨包 | 跨包 | 只可覆盖 |
protected static | 可以 | 不可以 | 跨包 | 跨包 | 只可覆盖 |
package (no modifier) static | 可以 | 不可以 | 包内 | 包内 | 只可覆盖 |
private static | 不可以 | 不可以 | 类内 | 不可以 | 只可覆盖 |
Class Variable:
Modifier | 包内对象访问 | 包外对象访问 | 类内访问 | super | 复写 |
public | 可以 | 可以 | 跨包 | 跨包 | 只可覆盖 |
protected | 可以 | 不可以 | 跨包 | 跨包 | 只可覆盖 |
package(no modifier) | 可以 | 不可以 | 包内 | 包内 | 只可覆盖 |
private | 不可以 | 不可以 | 类内 | 不支持 | 只可覆盖 |
Modifier | 包内对象访问 | 包外对象访问 | 类内访问 | 父类类名访问 | 复写 |
public static | 可以 | 可以 | 跨包 | 跨包 | 只可覆盖 |
protected static | 可以 | 不可以 | 跨包 | 跨包 | 只可覆盖 |
package (no modifier) static | 可以 | 不可以 | 包内 | 包内 | 只可覆盖 |
private static | 不可以 | 不可以 | 类内 | 不支持 | 只可覆盖 |