JAVA Keywords-关键字

93 阅读1分钟

Access modifier

 if a class is default or private, the subclass of it can not access the parent class.  

public class can be accessed in differenct package, the others can not.

except private class, other classes can be accessed in the same package.

Final keyword

final: can not be changed, can not be modified.

A final class can not be extends(can not be inherited), All the member functions in the class will implicitly be final methods, but not include variables.

A final method can not be overridden, but can be overloaded

A final variable can not be modified after initialization.

Static keyword

static method: it does not depend on any object, Static methods will be loaded as the class is loaded, so there's no  "this" keyword refer to the static method.   that's why a static method can not access to non-static method and non-static member variable in the class, because we need to "new" an object to invoke non-static methods and non-static member variable .

static variable: will be shared by all the objects, there's only one copy in the memory.Static variables will be loaded as the class is loaded.  static variables are stored in method area

**static code block:  **Static code blocks will be loaded as the class is loaded. A class can have many static code blocks and they can be placed anywhere in the class, but can not placed inside the method.it will only be executed once when the class is loaded. it usually is used to do initialization.  

Reference: www.cnblogs.com/dolphin0520…