Static (Static) Getting Keyword

A .static what's the use?

1. You can modify member variables, member methods, inner classes

2, can not be modified local variables (variables that belong to the class, not the method, the static class is defined on a).

Features II .static members

1, the object class is shared.

2, invoked by class name.

3, with the load classes being loaded.

4, takes precedence over the object exists .

It features three access methods .static

1, static methods can only be called a static member (static member variables, static member method).

2. Non-static method can call any member.

The reason: there is a method to the class priority, non-static objects belonging to methods, static methods belong to the class, advanced memory can not access backward memory, non-static method requires creating an object, called with an object.

Similarly: static method does not use this, because this is an object, the class takes precedence over the object exists, the object did not exist, so you can not write.

Four .Java in private or static methods can be overridden?

Can not

Cause; 1.private modification method can not be rewritten, private use only modified in this class.

2. static methods can be inherited, but it does not make sense, because sub-class is not rewritten but produce a static method in the parent class unrelated, the parent class is hidden.

So, the subclass can not inherit the parent class private or static methods

V. static abstract methods can be modified it?

No, there is no way abstract method body, adjusting the body's methods did not make sense.

VI. Static inner classes

Static inner classes can define static and non-static member variables and methods.

Non-static inner classes can only be defined non-static member (member variables, member method).

Internal static inner classes can only access static and non-static member can only be accessed outside the class static member.

Full access to non-static class, (internal static and non-static members, static members and external non-static members)

Seven, static code block

With the loaded classes and loading, preferentially executed in the constructor is performed only once, the role of: initializing a class driver is loaded.

Eight .static is a double-edged sword

Lee:

1 shared static method, space-saving, no need to store a copy of each object.

2. can be invoked through the class name. (Tools, singleton)

Disadvantages:

1. With the static method of class load and load, with the disappearance of class disappear, long life cycle.

2. Access limitations can only access the external static resources.

Guess you like

Origin www.cnblogs.com/fengtangjiang/p/11104526.html