Java Foundation 15-final, static keyword, and object class

Source: Internet
Author: User
Tags modifier

First, Final keywords

1. What is the final keyword

The advent of inheritance increases the reusability of code and facilitates development. However, there are problems, some classes do not want to be inherited after the description, or some of the methods in the class function is fixed, do not want subclasses to rewrite. But when the subclass inherits these special classes, it can rewrite the method, how to solve it?

To solve the above problems, you need to use a keyword final, final meaning is final, immutable. Final is a modifier that can be used to modify a class, a member of a class, and a local variable.

Features of 2.final

① decorated classes can not be inherited, but can inherit other classes

A ②final-modified method cannot be overridden, but the parent class is not in the final decorated method, and the subclass is overwritten to final.

③ modified variables are called constants, which can only be assigned once.

The value of the variable of the ④ reference type is the object address value, and the address value cannot be changed, but the value of the object property within the address can be modified.

⑤ modifies member variables and needs to be assigned before the object is created, otherwise an error is made. (when there is no explicit assignment, you need to assign a value to multiple constructor methods.) )

Second, the Static keyword

1. What is static

When you define a class, you have the appropriate properties and methods in the class. The properties and methods are called by creating this class of objects. When a method of an object is called, the method does not have access to the object's unique data, and the method creates the object somewhat superfluous. But do not create the object, the method can not be called, then you will think, then we could not create the object, we could call the method?

The above problems can be achieved by using the static keyword. static modifier , which is typically used to decorate a member of a class.

Features of 2.static

① member variables that are modified by static belong to a class and do not belong to an object of this class.

② members that are modified by static can and recommend direct access through the class name.

To access the format of a static member:

The class name . static member variable name

The class name . static Member Method name ( parameter )

Attention:

static content takes precedence over the existence of an object, can only access static, and cannot use this/super. Statically decorated content is stored in a static area.

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

This is because static modified methods and variables, belonging to classes, advanced method areas, non-static variables are not produced when they enter the method area.

The main method is a static method that simply executes the entry for the program, which does not belong to any one object and can be defined in any class.

3. Static constants

Above we know that the final modified variable is constant

The final and static modifiers are static constants.

Define the format:

public static final data type variable name = value ;

When we want to use static members of a class, we do not need to create an object to access it directly using the class name.

Attention:

each member variable in the interface uses the public static final decoration by default .

The member variable in all interfaces is already a static constant, and the assignment must be displayed because the interface does not have a constructor method. can be accessed directly with the interface name.

Interface Inter {    publicstaticfinalint COUNT = +;}

Third, object class

object is the root class of all classes in Java and is ancestor-level.

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

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

* Reference Data type: Class/interface/array, reference type is also called object class, so-called array variable name, should be exponential group object.

Common methods for 2.Object classes

* 1.protected void Finalize (): The garbage collector will call this method before looking back at an object, do a wrap-up operation, the method we do not go to invoke
* 2.getclass (), returns the true type of the current object. More in reflection.
* 3.hashcode (), returns the hash code value of the object, Hashcode determines where the object is stored in the hash table. The hashcode of different objects is not the same

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

* The Equals method in the object class, which itself is the same as the = = Symbol, is the memory address of the comparison object

* Official advice: Each class should override the Equals method, do not compare memory addresses, and lose the data we care about

* 5.toString (): Converts an object to a string.
* The object is printed by default, and the hexadecimal hashcode value of the object is printed.

* Official advise us: should each class should overwrite ToString to return the data we care about

Java Foundation 15-final, static keyword, and object class

Related Article

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.