Java Learning Lesson Eighth (constructors and their characteristics)

Source: Internet
Author: User

constructor function:


Constructor, which is the function called when building the creation object, is to initialize the object

features :

1. The function name and the class name are the same
2. You do not need to define a return value type
3. No specific return value

function: initializing an object



Note :

1. Features of the default constructor

2. Multiple constructors exist in the form of overloading


Code Demo:

Class Man{private int age;private String Name;man ()//defines a constructor for a man class {System.out.println ("Sun");} public void Fun () {System.out.println (name+ "" +age);}} public class main{public  static void  Main (string[] args) {mans Jo = new Man ();}} Print Sun,

PS: If a constructor is not defined in a class, then there is a default null argument constructor in the class


If the specified constructor is defined in a class, the default constructor in the class does not have a

Special NOTE: If you define a constructor that has a parameter, the form of the default constructor is also constructed, and the compiler no longer automatically generates

In fact: The default null argument function is: Man () {}


such as a class:
Class Test
{
}
Nothing, but in fact there is a test () {}, the compiler will compile the first to determine whether there is a write constructor, not automatically added, the default constructor is deleted
constructor, which is called when an object is created.


The difference between a general function and a constructor:


1. Constructor: When an object is created, the corresponding constructor is called and the object is initialized
General functions: When an object is created, it is called when a function call is required

2. Constructor, when object is created, call only once
General function: After the object is created, it is called several times to call


When do I use constructors?

When describing a thing, there is something in it that is defined in the constructor.
such as: When the object is created, there is a default age, name

Example 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) {mans Jo = new Man (); Jo.fun ();}}

Overloading of constructors:

class man{private int age;private String Name;man ()//define a man class constructor---------------|{                            |age = 20;                       |name = "John";                                            |}                                             |//objects are age before they are created, names |                             |man (int a,string s) |--------> Reload {|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) {mans 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 class in which the name of the function with the same name must be overloaded.

If a constructor is not called when an object is created, it means that the creation failed.

The memory allocation diagram for the constructor (as): Object to go into the heap, function to go into the stack


Constructor Details:

constructor, to make it easier to modify the information later, add set
Before the name of the constructor, no void is added, and then it is not a constructor, but it becomes a general function (the feature of the constructor 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 man (int a,string s) {ages = 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) {mans Jo = new Man (); Jo.set ("BLF2"); Jo.fun ();}}

The problem with this piece of code is:

Because no constructors are defined.

The default constructor is that the code is added automatically when the constructor is not created, but the code above has a constructor, but there is no man () {} Looking for a type of constructor, so the error

Java Learning Lesson Eighth (constructors and their characteristics)

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.