Constructors, static keywords, and final keywords

Source: Internet
Author: User

The constructor is also called a constructor.

Each class has at least one constructor.

Constructor is a special method with the following features.

(The Method Name of the constructor must be the same as the class name.

(The constructor does not have a return type and cannot be defined.

(The main function of constructor is to complete object initialization.

(Constructor. If no constructor is defined when a class is defined, the compilation system automatically inserts a default constructor without parameters. This constructor does not execute any code.

(The constructor can be overloaded and distinguished by the number, type, or order of parameters.

(6) subclass cannot inherit the constructor.

If the constructor is not explicitly written to a class, an implicit constructor will exist in this class by default.

Therefore, we recommend that you write a non-parameter constructor when defining a class.

When instantiating an object, it does not indicate which constructor to call. By default, a constructor without parameters is called.

Definition constructor:

System. out. println ("I Am a lowercase letter:" +}

Call the constructor:

 

                                        Person p1 =  Person();         Person p2 =  Person("a");           }

 

Static

Load as the class is loaded

Takes precedence over the existence of objects.

Shared by all objects

Can be called directly by Class Name

It can be used to modify methods and member variables.

Usage notes

Static methods can only access static members, but non-static methods can access static members.

Static methods cannot be used

Not used

Use

Static

No

When is it suitable for use?

Generally used when writing tool Methods

Example:

Person () System. out. println ("construction method without Parameters "}
String name; String sex; String address; name = "Xiao Li"; address = "Heaven and Earth"; System. out. println ("dinner" name = "Xiao Li"; address = "Heaven and Earth"; System. out. println ("Walking "}

Call:

Person p = p. name = "Xiao Ming"; Person. name = "Xiao Li"; System. out. println (}

Final

Java

Final
Final
Final
Final
Note: You can use final when a class does not need a subclass.

Final class:

                 B  A           }

Final method:

Test () System. out. println ("I'm the final method" B test () System. out. println ("I'm the final method "}

Final variable:

                       number=20       age;        A(          .age=                 number++;     }

Final constructor:

A () System. out. println ("constructor "}

Final reference data type:

  StringBuilder sb =  StringBuilder("123"         sb.append("456");         

Although the final modified variable is called a constant, the value cannot be modified. However, the value of the referenced variable can be modified, but the address cannot be modified.

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.