Java keyword Summary (1)

Source: Internet
Author: User

1 Final data:
1> final modification before the basic type indicates that the modified variable is a constant and cannot be modified. A static or final field indicates that only a bucket cannot be changed.
2> when final is used for object applications, final keeps the application unchanged. Once the reference is initialized to point to an object, it cannot point to another object.
3> final method: first, lock the method to prevent the inherited class from modifying its meaning. Second, ensure that the behavior of the method remains unchanged in inheritance and will not be overwritten. All private methods in the class are implicitly specified as final.
4> final parameter: for variables of the basic type, this does not have any practical significance, because variables of the basic type are passed values when calling methods, that is to say, you can change this parameter variable in the method without affecting the call statement. However, it is very practical for the object variable, so that the object variable is passed as a reference when being passed, in this way, your modification to the object variable in the method will also affect the object variable of the Call Statement. When you do not need to change the variable as a parameter in the method, use final to declare it, it will prevent you from accidentally modifying the call method.
5> final class: When a class is defined as final, it indicates that the class cannot be inherited.

2 This and super
1> A criminal parameter name in your method has the same name as a member of the current object, so as not to be confused, you need to explicitly use the this keyword to specify that you want to use a member. The usage is this. the member name without this is the criminal parameter. In addition, you can also use this. method name to reference a method of the current object, but this is not a must. You can directly use the method name to access that method.
2> when the method of the parent class is overwritten, you can use super to call the method of the parent class. If the method of the parent class can be called by the quilt class, you can use it like this and call it by using the member name in the super. parent class.
3> super and this follow the appropriate parameters directly, so its meaning changes. Super is used to call constructors with the same form in the parent class. This is used to call constructors with the same parameters.
4> This usually refers to the current object, and super usually refers to a class with a parent class.
3 static
1> A method is usually defined as static in a class, that is, it can be called directly without the object of this class.
2> static variables are similar to static variables. All such instances share this static variable. That is to say, only one storage space is allocated during class loading, and all such objects can control this storage space. Of course, final is another matter.
3> static variables take precedence over any other non-static variables, regardless of their order.
4> static {} is used for explicit static variable initialization. This code is only initialized once, and when the class is loaded for the first time.
5> when inheritance is involved, the static variable of the parent class will be initialized first, and then the static variable of the Child class.
6> A common class cannot be declared as static. Only one internal class is allowed. In this case, the static internal class can be directly used as a common class without the need to instance an external class.
4 abstract Abstract
1> it is used in the declaration of classes to indicate that a class cannot be instantiated, but can be inherited by other classes. An abstract class can use abstract methods. abstract methods do not need to be implemented, but must be implemented in subclasses. It is used to provide general information to sub-classes. An abstract class can contain anything that a normal class contains, that is, classes and instance variables, and methods with any modifiers. Only abstract classes may have abstract methods. If a non-abstract class contains an abstract method, a compilation error occurs.
Example: If one of the following statements is true, an abstract method is provided:
A. It explicitly declares an abstract method.
B. It inherits an abstract method from its direct parent class.
C. The parent excuse of a direct class declares or inherits a method of it (this must be abstract)
2> If the private, static, and final methods are declared as Abstarct, a compilation error occurs. It is impossible to reset a private method, because an Abstarct private can never be implemented. Static methods are always available, so there must be an implementation; static abstract will never be implemented. The final method cannot be reset, so there is no implementation of the final abstract method.

 

 

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.