A detailed explanation of this, super, static, and final keywords in Java __java

Source: Internet
Author: User
Tags modifier

What is this: represents the current object. This has two main locations:
Constructor:
represents the currently created object
method: Which object calls the method in which this is, then this represents which object.
When an object is created, the JVM assigns a reference to itself: this

Use this:
1, to solve the member variables and parameters (local variables) between the two semantic, must be used;
2, the same kind of instance methods between the intermodulation (at this time can be omitted);
3, pass this as the parameter to another method;
4. Use this as the return value of the method (chained method programming);
5, the construction resets the intermodulation, this as (parameter) must write in constructs the method first line;
6, static can not be used with this.
When the byte code is loaded into the jvm,static member already exists, but the object has not been created yet, no object has no this.

There are two ways to create an object and set an initial value on an object:
Method One:
first create an object through the parameterless constructor, then invoke the corresponding setter method through the object.
mode two: directly call the constructor with the parameter, the created object, there is an initial value.


You can do the same thing through the setter method and through the constructor, so how do you choose:
1,
If there is a constructor with parameters, the mode two relatively concise;
2, If you need to initialize multiple data when building objects, if the use of mode two, then the constructor to provide n parameters, too many parameters, not intuitive. At this time the way one, simple and clear.

3, Special circumstances , if you need a value to build the object, then you need to use the constructor (such as building a circle object, you need to know the value of the radius first).




Super:

now there is a requirement: in a subclass of a method, remove the method that is overridden with the parent class, how to invoke the.
Solution at this point: Use the Super keyword.


What is super:
This:
current object, water call this is the method, this is which object;
Super: The parent class object of the current object.

Subclass initialization procedure: The process of creating child class objects
The parent class object is created before the subclass object is created.
Before calling the child class constructor, the parent class constructor is invoked in the subclass constructor, and the default invocation is the parameterless constructor of the parent class.
1. Subclasses cannot exist if the parent class does not exist a constructor that can be accessed by the quilt class.
2. If the parent class does not provide a parameterless constructor, the subclass must display a constructor that invokes the parent class with parameters through the Super statement.

Use scenario for Super Keywords:
1, you can use the Super solution subclasses hide the parent class field, which we generally do not discuss, because the destruction of encapsulation;
2, in the subclass method, call the parent class coverage method, leads to the super example, must use super at this time;
3, in the subclass constructor, call the parent class constructor, at which point you must use the Super statement: Super ([argument]).

Static cannot coexist with this, super.




Static:

Why the static is used:
States and behaviors should have objects and types:
Some states and behaviors should belong to the object, different objects, states and behavior can not be the same;
Some states and behaviors should belong to the type and not to the object.
to solve this situation, you need to use static to resolve it.


The static modifier represents a statically decorated field, method, inner class, and its decorated members belong to a class, which means that the static-decorated resource belongs to the class, not to the object level.
The true role of static: to distinguish between fields, methods, inner classes, and initialization block code that belongs to the object or to the class itself.


static modifier features:
1. Static modified members (fields and methods), loaded with the loading of the class;
When the JVM puts the bytecode into the JVM, the static-decorated member already exists in memory.
2. Precedence over the existence of the object;
Objects are created by the key to our new
3. The members of the static modifier are shared by all objects of that type;
A static member can be accessed based on any object created by the class.
Note: On the surface, the static member is accessed through the object, and its nature is still accessed with the class name, with no relation to the object
4. Access the static member directly using the class name.
Because the members of the static modifier are directly subordinate to the class and are not part of the object, you can access the static member directly using the class name.

access to class members and instance members:
Members in a class: field, method, inner class
Class Members:Members with the static modifier
Instance members:No members with a static modifier

class members can access only class members, and instance members can access only instance members;

Class Members:
Directly belongs to the class, you can access the static field and the static method through the class;
Instance members:Only belongs to the object, accessing the Non static field and the non static method through the object;
(An object can actually access a class member, but the underlying class name still accesses it)

In the static method, only static members can be invoked.
Non-static methods, you can access static members, or you can access instance members.

When are the fields and methods defined as static:
If this state/behavior belongs to the whole thing (class), it is used directly by the static modifier and is shared by all objects.

In the development, often putTool class methods use static adornments
If you do not use the static adornment, these methods belong to the object of the class, we have to create the object and then call the method, in the development of the tool object only need a copy,
It is possible to create N objects, at which point we tend to design the class as a single example, but there is still a bit of trouble.
So, generally, in the development of the design tool method, in order to invoke simple, we use static.

use of static members:
Advantages:
The object's shared data is stored in a separate space, saving space, there is no need for each object to store a copy, can be directly called by the class name.
Disadvantages:Life cycle is too long, static more than the memory.




Final modifier:
Why you need to use the final modifier:
The biggest disadvantage of inheritance is that it destroys encapsulation, the subclass can access the implementation details of the parent class, and can modify the implementation details in the form of a method overlay.


Final itself means " final, immutable ", and it can modify Non-abstract, non-abstract methods and variables.
Note: The construction method cannot use the final modification because the construction method is final and must not be inherited.


Final Modified class: represents the final class, which can no longer have subclasses;
Final modification of the method: the Ultimate method, the method can not cover the quilt;
Final Modified variable: The ultimate variable, constant, which can only be assigned once and cannot be assigned again.


More code is recommended to achieve, so the memory deeper.

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.