Common keyword parsing in Java _java

Source: Internet
Author: User
Tags modifier

This article mainly aims at the Java Common key word final,static,super,this to carry on the detailed discrimination, in order to facilitate the reader to consult. Specifically as follows:

First, final

1) modifier Class (Class):

Indicates that this class cannot be inherited.

2) Modifying method (methods)

Indicates that this method method cannot be overridden (@override).

3) modified variable (variable)

Cannot be changed after representing the value of this variable, often used to decorate immutable constants

Note: final literal meaning is "last", "Cannot change" meaning, combine meaning memory more convenient.

Second, static (the second to third method commonly used)

1) modifier Class (Class):

(not commonly used) can only be used to modify the inner class, which is not allowed to be declared by ordinary classes

2) Modifying method (methods)

The static method belongs to all classes (shared among objects), not to an object. You do not need to create an object, you can call this method by using the class name.
(When the program executes, the byte code of the class is loaded into memory, and when the class does not create the object, the class variable is already allocated memory, and the instance variable allocates the memory space when it creates the concrete object.) )

3) modified variable (variable)

Like 2, a variable is a class-level share without creating an object, but the value of a variable is integral, that is, object a changes the value of a static variable, and object B operates on the changed static variable.

Note: A code block decorated with static is called a static code block, and static code blocks are executed when the JVM loads the classes, regardless of where they are placed, they are executed first, and then the normal blocks of code are executed. There can be multiple static code blocks, which are sequentially executed by the JVM in the order in which each code block is executed once.

the This and super keywords cannot appear in a static method.

Iii. Super: (refers to a reference to the parent class)

1 is related to class (parent class, also known as Super Class):

By super (parameter 1, Parameter 2, ..., parameter n), invoke the parent class constructor;

2) associated with method (methods)

Super. Methord (parameter 1, Parameter 2, ..., parameter n) invokes the method that the parent class is overridden;

3) associated with variable (variable)

Super.variable, calling a variable of the parent class

Note:super must be used in the first line of the subclass construction method ;

when the parent class does not have a constructor without parameters, the subclass needs to use Super to explicitly invoke the constructor of the parent class, and Super refers to a reference to the parent class

Four, this: (a reference to the current object)

1 Class-Related:

This (parameter 1, Parameter 2, ..., parameter n), which is a normal constructor, can only be a constructor (other) call constructor (this), the normal function cannot call the constructor.

2) methods (method) Related:

Method that invokes the current object

3) Variable (variable) Related:

Call the current object's variable

Note: this(); and super (); an explicit call constructor can only be placed in the first row of the construct;

This.method.. () Super.method. (); Can be placed in any place as a common method of making;

Constructors and methods use the keyword this is a big difference. method references this to an instance of the class that is executing the method. Static methods cannot use the This keyword, because static methods do not belong to instances of the class , so there is nothing to point to. This of the constructor points to another constructor in the same class with a different argument list.

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.