Section 10 Statics, Running Order and Access Level

来源:互联网 发布:wumpus java 游戏编码 编辑:程序博客网 时间:2024/05/22 06:52

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; // trueiWrap.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 2004String.format("%tr", new Date());04:23:13 PMDate 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。

ModifierInterface继承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 {@Overridepublic 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方法。

总结:

  1. 接口的方法和常量的访问权限由接口的访问权限确定,要么包内,要么跨包。
  2. abstract方法可覆盖(重名,没有body所以算不上复写)。
  3. default方法可以继承,可以复写。继承可以扩大default方法的访问权限。
  4. static方法可覆盖,不能被继承,更不能被复写。
  5. 常量可继承,可覆盖(重名)。

Class:

public跨包访问no modifier包内访问

final类不能被继承。

abstract类不能被实例化。类中含有abstract方法必须声明为abstract类。abstract类中可以有concrete方法。

Class Method:

Modifier包内对象访问包外对象访问类内访问super复写public可以可以跨包跨包跨包protected可以复写后可以跨包跨包跨包package (no modifier)可以不可以包内包内包内private不可以不可以类内不支持只可覆盖
复写方法时可以increase visibility,但不能reduce visibility。可以将concrete复写成abstract。

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不可以不可以类内不支持只可覆盖
no modifier限制了在package内访问。protected是继承到包外子类中访问。
在static context中是不能使用generic的。
0 0