Lesson 8 (constructor and Its Features)

Source: Internet
Author: User

Lesson 8 (constructor and Its Features)

Constructor:


A constructor is a function called when an object is created. It initializes the object.

Features:

1. The function name and class name are the same
2. the return value type does not need to be defined.
3. No specific return value

Purpose: Initialize the object.



Note:

1. Features of default constructor

2. Multiple constructors exist in the form of overloading


Code Demonstration:

Class man {private int age; private String name; man () // defines a man class constructor {System. out. println ("sun");} public void fun () {System. out. println (name + "" + age) ;}} public class Main {public static void main (String [] args) {man jo = new man () ;}} print sun,

PS: If no constructor has been defined in a class, a default null constructor will appear in the class.


If the specified constructor is defined in the class, the default constructor in the class will no longer exist.

Note: If a constructor with parameters is defined, the constructor is also constructed by default and will not be automatically generated by the compiler.

Actually: the default null Parameter Function is man (){}


For example, a class:
Class test
{
}
There is nothing, but there is actually a test () {} in it. during compilation, the compiler will first determine whether to write the constructor. If no constructor is automatically added, it will delete the default constructor.
The constructor is called when an object is created.


Differences between common and constructor functions:


1. constructor: when an object is created, it calls the corresponding constructor and initializes the object.
General functions: After an object is created, it is called only when the function is called.

2. constructor, called only once when an object is created
General functions: After an object is created, it is called several times if you want to call it several times.


When can I use constructors?

When describing a thing, it contains some content, which is defined in the constructor.
For example, when an object is created, it has a default age and name.

Sample Code:

Class man {private int age; private String name; man () // defines a man class constructor {age = 20; name = "john" ;}public void fun () {System. out. println (name + "" + age) ;}} public class Main {public static void main (String [] args) {man jo = new man (); jo. fun ();}}

Reload the constructor:

Class man {private int age; private String name; man () // defines a man class constructor ------------- | {| age = 20; | name = "john "; |}| // The object has an age and name before it is created | man (int a, String s) | --------> heavy load {| age = a; | name = s; |} | man (String s) | {| name = s; |} --------------- | public void fun () {System. out. println (name + "" + age) ;}} public class Main {public static void main (String [] args) {man jo = new man (); jo. fun (); man jj = new man (1, "BLF"); jj. fun (); man jjj = new man ("BLF2"); jj. fun (); // print BLF 0 }}

PS: A function name with the same name appears in a class, which must be reloaded.

If the constructor is not called when an object is created, the creation fails.

The memory distribution diagram of the constructor (for example): objects need to be added to the heap, and functions need to be added to the stack.


Constructor details:

To facilitate future modification of information, add the set
You cannot add void or so on before the name of the constructor. After the constructor is added, it is not a constructor, but a general function (constructor features 2)


Error code example

Class man {private int age; private String name; man (int a) {age = a ;}// the object has an age before it is created. The name is man (int, string s) {age = a; name = s; fun ();} public void set (String s) {name = s;} public void fun () {System. out. println (name + "" + age) ;}} public class Main {public static void main (String [] args) {man jo = new man (); jo. set ("BLF2"); jo. fun ();}}

The problem with this code is:

Because no constructor is defined.

The default constructor is automatically added when the Code does not create a constructor. However, the above Code has constructor, but no man () {} Type constructor is found. Therefore, an error is returned.

Related Article

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.