Java Fundamentals 15-final, static keyword and Object class

1. final keyword

1. What is the final keyword

The emergence of inheritance improves code reusability and facilitates development. But there are also problems. After some classes are described, they do not want to be inherited, or some methods in some classes are fixed and do not want to be rewritten by subclasses. But when subclasses inherit these special classes, they can rewrite the methods in them. How to solve it?

To solve the above problems, you need to use a keyword final , which means final and immutable. final is a modifier that can be used to modify classes, members of classes, and local variables.

2. Features of final

① Modified classes cannot be inherited, but can inherit other classes

②Final modified methods cannot be overridden, but there is no final modified method in the parent class, and the subclass can add final after overriding.

③ Modified variables are called constants, and these variables can only be assigned once.

④The variable value of the reference type is the object address value. The address value cannot be changed, but the object attribute value in the address can be modified.

⑤  To modify member variables, you need to assign values ​​before creating the object, otherwise an error will be reported. (When there is no explicit assignment, multiple constructors need to assign values ​​to them.)

Second, the static keyword

1. What is static

When defining a class, the class will have corresponding properties and methods. The properties and methods are called by creating an object of this class. When a method of an object is called, and the method does not have access to the specific data of the object, it is somewhat redundant for the method to create the object. But if you don't create an object, the method can't be called. At this time, you will think, can we call the method without creating the object?

The above problems can be achieved through the static keyword. static is a static modifier that is generally used to modify members of a class.

 2.Static features

①The member variable modified by static belongs to the class, not an object of this class.

 

 ② Members modified by static can and are recommended to be accessed directly through the class name.

Format for accessing static members:

class name . static member variable name

class name . static member method name ( parameter )

Notice:

 Static content exists in preference to objects, only static can be accessed, and this/super cannot be used . Statically decorated content is stored in the static area.

In the same class, static members can only access static members

 This is because the statically modified methods and variables belong to the class, the advanced method area, and the non-static variables are not generated when they enter the method area.

 The main method is a static method only for the program execution entry. It does not belong to any object and can be defined in any class.

3. Static constants

As we know above, final modified variables are constants

That final and static co-modify are static constants

Definition format:

public static final data type  variable name  = value ;

When we want to use the static members of a class, we don't need to create an object, just use the class name to access it.

Notice:

Each member variable in the interface is decorated with public static final by default.

All the member variables in the interface are static constants. Since the interface has no constructor, the assignment must be displayed. It can be accessed directly with the interface name.

interface Inter {
    public static final int COUNT = 100;
}

3. Object class

Object is the root class of all classes in java and is the ancestor.

1. Why is Object the root class of all classes, and what exactly is the Object class?

* object itself refers to the meaning of the object. We found that all objects have some common behaviors, so we abstract a class object object class, others will inherit from the object class, and also have the methods in the object class

* Reference data type: class/interface/array, the reference type is also called the object class, the so-called array variable name of the array should refer to the array object.

2. Common methods of the Object class

* 1.protected void finalize(): Before the garbage collector looks back at an object, it will call this method to do a tail-sweeping operation. We do not call this method
* 2.getclass(), which returns the real type of the current object. Mostly used in reflection.
* 3.hashcode(), returns the hash code value of the object, the hashcode determines the storage location of the object in the hash table. The hashcode of different objects is different

* 4.equals(), compare the current object (this) with the parameter obj,

* The equals method in the object class, which is the same as the == symbol, compares the memory address of the object

* Official suggestion: every class should override the equals method, don't compare memory addresses, and lose the data we care about

* 5.toString(): Convert an object to a string.
* By default, the object is printed, and the hexadecimal hashCode value of the object is printed

* Officially suggest us: every class should override toString to return the data we care about

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325096606&siteId=291194637