JAVA learning notes-Introduction to several common keywords static, final, this, super, learning notes static

Source: Internet
Author: User

JAVA learning notes-Introduction to several common keywords static, final, this, super, learning notes static

I. static

Static (static) can be placed before classes, methods, and fields.

Generally, when a class is created, the class's appearance and behavior are described. Unless the class object is created with new, no object is actually obtained. When you execute new to create an object, the data storage space is allocated and its method can be called by the outside world. However, the above methods cannot be solved in two cases. 1:You only want to allocate a single bucket to a specific domain.Instead of considering the number of objects to be created, or even not creating any objects at all. Another scenario is,It is expected that a method will not be associated with any object of the class containing it.That is to say, this method can be called even if no object is created.

The static keyword can meet the preceding requirements. When a thing is declared as static, it means that this domain or method will not be associated with any object instance of the class that contains it. Therefore, even if no object of a certain type is created, you can also call its static method or access its static domain.

Before defining the static keyword, you can set the field or method to static.

1) static class

When a class is defined as static, it indicates that it is a static class. If a class is declared as a static class, there is only one situation: static internal class (nested class ). For Nested classes, I will explain them in detail elsewhere.

2) static Method

Generally, when a method is declared as static, it indicates that the method can be called directly through the "class name. Method Name" method, without the need to create an object of this class, but there are some restrictions:

· Static methods can only call static methods. They cannot call non-static methods in static methods;

· Static methods can only access static data members

· Static methods cannot reference this or super in any way

3) static data member

When a data member is declared as static, the data is unique, because for each class, A static field has only one bucket (a non-static field indicates that each object has a bucket). When this field is changed, the field will be modified in all parts of the program. It is also very convenient to reference. You can directly "class name. static variable name.

Ii. final

Using the final keyword in JAVA usually indicates that this place cannot be changed.

1) final data

Final data is used to indicate a piece of data that cannot be modified. For the basic data type, final keeps the value unchanged;For object reference, final makes the reference constant, that is, once the reference is initialized to point to an object, it cannot be changed to point to another object, but the object itself can be modified.. Compared with static, static indicates that this field has only one bucket, but its value can be changed. final indicates that this is a constant. Therefore, both static and final fields occupy only a segment of storage space that cannot be changed, similar to "global variables", which are expressed in uppercase.

2) final Method

You can use the final method to lock the method to prevent any inheritance class from modifying its meaning (not overwriting ). The final method is faster than the non-final method, because it has been statically bound during compilation and does not need to be dynamically bound, but the efficiency may cause other problems, now we no longer need the final method for optimization. We can set the method to final only when we want to explicitly disable overwriting.

In addition, private methods are implicitly declared as final and cannot be overwritten. When you try to override the private method of the parent class in the subclass, instead of overwriting, you create a new method with the same name as the method of the parent class.

3) final class

When a class is defined as final, it indicates that we do not intend to inherit the class and do not allow others to do so. This class will not have child classes.

 

3. this

When you need to obtain a reference to the current object within the method, you need to use the this keyword.This keyword can only be used inside a method, indicating reference to the object that calls the method.. This keyword has two main applications:

1) return a reference to the current object, so you can call other methods or member variables of this class in the instance code: this. method name, this. member variable name;

2) Pass the current object to other methods, so that the constructor can call other constructor of the same class. this statement must be placed in the first line of the constructor during the call. In addition, this can only call one constructor, but not two.

 

Iv. super

The keyword super has two purposes:

1) Call the constructor of the parent class:

Super (); // call the parameter-free constructor of the parent class (default constructor). Generally, subclasses call the default constructor of the parent class implicitly.

Super (parameter); // call the constructor of the parent class that matches the parameter,

The super statement must appear in the first line of the subclass constructor. This is the only method that explicitly calls the parent constructor. In any case, when constructing an instance of a class, the constructor of the parent class is called one by one along the inheritance chain to ensure initialization.

2) method for calling the parent class:

Super. Method Name (parameter );

Note: this and super cannot appear in a constructor at the same time;

This () and super () cannot be used in the static environment;

It can be understood that this points to the current object, and super points to the parent class.

 

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.