[Javase Learn notes]-7.5 a few details to note about the constructor function

Source: Internet
Author: User

In this section, we'll simply say a few details that you need to be aware of when using constructors.


Through our previous sections, we have a fairly clear understanding of constructors, and when we create objects, we call constructors. So what are the details we need to be aware of when defining and invoking constructors?


The first detail to note: constructors and Set methods

Look at two function codes:

Class Person{private string Name;private int age;//constructor, initialization of Nameperson (string n) {name = N; System.out.println (name+ "age" +age); General function, set namepublic void SetName (String n) {name = n;}}
We see that there are two functions, the first is the constructor, the second is the general method, and they are all two to set the name of the content, then we can use the constructor instead of the set method, it is clear that this is not, because we have previously learned the difference between the constructor and the general function, The Set method is a general function. The constructor simply initializes the name once and then no longer works, and the set method can be called at any time when we need to change the name.


The second detail to note: constructors can call general functions, but general functions cannot call constructors directly.

Let's look at an example:

Class Person{private string Name;private int age;//constructor, initialize Nameperson (string n) {setName (n); System.out.println (name+ ":" +age);} General function, set namepublic void SetName (String n) {name = n;}}
Compile the pass, let's test it.

Class Persontest {public static void main (string[] args) {person p = new Person ("Johnny");}}
Results:

It is clear that it is possible to call a general function in a constructor.

When we call the constructor in the set method:

Class Person{private string Name;private int age;//constructor, initialization of Nameperson (string n) {name = N; System.out.println (name+ ":" +age);} General function, set namepublic void SetName (String n) {person (n); name = n;}}
Results:

We see that the compilation is straightforward, so the constructor cannot be called directly in the general function.


A third detail to note: General methods with the same name as the class names

Let's look at a function like this:

Class Person{private string name;private int age;void person (string n) {name = N; System.out.println (name+ ":" +age);}}
Test:

Class Persontest {public static void main (string[] args) {person p = new Person ("Johnny");}}
Results:

We see a hint that the constructor cannot be applied to a given type, there are no arguments, but our code has the parameter "Xiao Qiang", which means that the function we create is not a constructor, but a general function, and the program does not find the corresponding constructor in the person class. Because the constructors are not defined in our code, there is only a default constructor, and the default constructor does not have any parameters. So we must pay attention to this phenomenon in the future programming process.


The fourth detail to note: There is a return statement in the constructor.

We look at the code to explain the problem

Class person{private String name;private int age; Person (String N,int a) {if (a<0) {System.out.println ("initialization is illegal! "); return;} Name = N;age = ASystem.out.println (name+ ":" +age);//We learned in the function that the return statement in a function that has no return value can be omitted}}
Let's test it.

Class Persontest {public static void main (string[] args) {person p = new person ("Xiao Qiang",-1);}}
Results:


We find that the program compiles and runs normally, and the return statement jumps out of the constructor, which means there is a return statement in the constructor.


In this section we learn the details that are often needed in the four constructors, and we pay more attention to it later in the programming process.

[Javase Learn notes]-7.5 a few details to note about the constructor function

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.