JAVA class and object (3) ---- explanation of class definition keywords, java ----

Source: Internet
Author: User

JAVA class and object (3) ---- explanation of class definition keywords, java ----

 

Static indicates static. It can modify attributes, methods, and code blocks.

1. static modifier attributes (class variables ),This attribute can be accessed by class name. attribute name, that is, to make this attribute a class variable of this class, which is shared by this class object. This attribute is fully public. (The common class variables are irrelevant to the object and only related to the class ).

Note: The instance variables in the class are initialized when the object is created. The static modified attributes, that is, class variables, are created and initialized when the class is loaded, class loading is performed once. That is, the class variable is created only once.

2. static modification method (static method ),This method becomes the public method of the entire class and can be accessed by class name. method name.

Note:Static modification method, does not directly access non-static (static) members (including methods and attributes) in this class, static (static) of this class) methods can access static members (including methods and attributes) of this class and call static methods. Use static methods with caution. This keyword cannot appear in static methods.

Note:The parent class is a static method, and the Child class cannot cover non-static methods. If the override rules are met, in the parent and child classes, static Methods in the parent class can be overwritten by static methods in the quilt class, but there is no polymorphism.

Note:In the Parent and Child classes, static methods can only be overwritten by static methods. In the Parent and Child classes, non-static methods can only be overwritten by non-static methods.

 

3. Modify the initial code block in static mode,This code block is executed only once when the class is loaded. You can use a static initial code block to initialize a class. The dynamic initial code block is written in "{}" in the class body. This code block is run in the initialization attribute of the generated object.

 

Supplement:When will the class be loaded?

Classes are loaded when an object is created, and classes are loaded when static methods or access static properties in the class are called. When a subclass is loaded, the parent class must be loaded first. The class loading has a delay loading principle, which is only loaded when it is required.

 

Final modifier, which can be used to modify variables, methods, and classes
1. final modifier variable
Variables modified by fianl will become constants (constants should be capitalized). Once the value assignment cannot be changed, fianl constants will not have default values, the final modifier is often used together with the static modifier when the initialization is a value assignment.

2. final modification methodThe method modified by final cannot be overwritten by its subclass, and the method stability cannot be overwritten.
3. final modifier classThe class modified by final cannot be inherited. Methods In the final class are also final.

Note:(1) final cannot be used to modify constructor methods. If a constant attribute exists in the parent class, the class of the parent class will not be loaded when the constant attribute is used in the subclass.

(2) In unchanged mode, once an object is created, its attributes will not change. Use final to modify attributes, final to modify classes (strong immutable mode), and final to modify attributes (weak immutable mode ).

 

 

Abstract modifier, which can be used to modify classes and Methods

1. abstract modifier class,This class will become an abstract class. This class will not generate an object instance, but it can be used as the object variable declaration type, that is, the compile-time type. The abstract class is like a class of semi-finished products, the subclass must inherit and overwrite the abstract methods.

2. abstract modification method,This method is converted into an abstract method, that is, it is not implemented only by declaration (Definition), and the Implementation part is replaced. Subclass inheritance is required (overwrite ).

Note:(1) classes with abstract methods must be abstract classes. However, not all abstract methods are abstract methods, but also specific methods.
(2) abstract modifiers must be placed before the class name when modifying the class.

(3) abstract modifier requires its subclass to overwrite (implement) This method. You can call the method after the subclass overwrites (implement) in a multi-state manner. That is to say, the abstract method must be implemented in its subclass unless the subclass itself is also an abstract class.

Note:The parent class is an abstract class with an abstract method, so the subclass inherits the parent class and implements (overwrites) All the abstract methods in the parent class, A subclass can create an instance of an object. Otherwise, the subclass must be an abstract class. Abstract classes can have constructor methods, which are the constructor of the parent class (abstract class) that a subclass needs to call when constructing a subclass object.

Final, abstract, private, abstract, static, and abstract are modifiers that cannot be put together, because abstract modifiers must be implemented (overwritten) in their subclasses ), can be called in a multi-state manner. The above modifier does not cover this method during the modifier period. final cannot cover this method, and private cannot inherit from this method, so it cannot be overwritten, static can be overwritten, but it will call the method of the compile-time type during the call, because the method of the parent class is called, and the method of the parent class is an abstract method, the modifier cannot be put together.

Abstract METHODS represent certain standards, standards, and functions, implement the function in the subclass (The subclass inherits the parent class and must provide the Implementation of the abstract method inherited from the parent class ).
Methods can be implemented unexpectedly at a time, or define certain standards for subclass implementation. This method can be defined as abstract. (Abstract)

 

 

Public (public access permission ):

This is a loose access control level. If a member (including member variables, methods, constructors, and so on) or an external class is modified with a public access controller, this member or external class can be accessed by all classes, regardless of whether the parent class or external class is in the same package or whether it has a parent-child inheritance relationship.

Private (current class access permission ):If a member in the class (including member variables, methods, constructors, etc.) is modified using a private access controller, the member can only be accessed within the current class. Obviously, this access control operator is most suitable for modifying member variables. using it to modify member variables can hide member variables within this class. Protected (subclass access permission ):If a member (including member variables, methods, constructors, etc.) is modified using the protected access controller, the member can be accessed by other classes in the same package, it can also be accessed by sub-classes in different packages. In general, if you use protected to modify a method, you usually want its subclass to override this method. Default (package access permission ):If a member (including member variables, methods, constructors, etc.) in a class or an external class does not use any access control operator modifier, it is called the package access permission, the default access control member or external class can be accessed by other classes under the same package.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.