Java constructor Detailed serial 39-

First,

1. The multi-line comments: CTRL + shift + /

2. When a class does not define any method of construction, the system defaults to class provides a constructor with no arguments, the constructor is called the default constructor.

 

public class D39_ {

  public static void main(String[] args) {

    new User();

  }

}

Compile, we had defined a class User, there is no constructor.

3. When a class explicitly defines the constructor out, then the system is no longer provided by default default constructor. Recommended the development, the manual provides a no-argument constructor for the current class, because no-argument constructor too common.

4. The supporting construction method overloading, in the preparation of a plurality of class constructor, this clearly constitutes a plurality constructor method overloading.

The role of the constructor:

(1) Create Object

(2) while creating an object, initialized instance variable memory space

Member variables of instance variables, are object-level variables, this variable must have in order to have an object instance variables. No manual assignment of instance variables, the system default assignment, then the default assignment the West Wing when it is completed it?

The class is loaded when it?

No, because only when the class loader loads the code snippet, has not had time to create objects, so in this case there is no instance variable initialization. In fact, the strength of the process memory space is too variable construction method performed in complete open, the completion of the initialization, the system default assignment when the process is done in the constructor execution of the assignment.

Instance variable Default:

byte / short / int / long are 0

/ Float / double are 0.0

boolean is false

6. shortcuts:

(1) CTRL + o: In a class, if the element too, can find the elements we want through this shortcut.

(2) Hold down the CTRL key, move the mouse and then sent to see the elements appear underlined when to start clicking, you can jump to the line of code.

(3) quickly generate Constructor: Right - Code - Use field generated constructor

 

Package com.bjpowernode.java_learning; 


public  class D39_ { 

  public  static  void main (String [] args) { 

    // Create User object class User call the constructor to create complete object 

    // The following procedure creates four objects, the constructor calls as long as the object is created, and it must be opened up in the "heap" in the memory space 

    String s1 = "jfsiauhf" ; 

    int I1 = 5 ; 

    the User U1 = new new the User (s1); 

    the User U2 = new new the User ( I1); 

    the User U3 = new new the User (); 

    System.out.println ( "fjisf" ); 

    // call the method with the static 

    u3.sum ();

    // method calls without the static 

    / ** 

     * Javadoc format 

     * / 

    u3.getAge (); 

   

    the Account ACT1 = new new the Account (); 

    act1.getActno (); 
  } 
}


 
package com.bjpowernode.java_learning;

​

public class Account {

  //账号

  private String actno;

 

  /**

   * @param actno

   * @param balance

   */

  public Account(String actno, double balance) {

    super();

    this.actno = actno;

    this.balance = balance;

  }

​

  //余额

  private double balance;

 

  public String getActno() {

    return actno;

  }

​

  public void setActno(String actno) {

    this.actno = actno;

  }

​

  public double getBalance() {

    return balance;

  }

​

  public void setBalance(double balance) {

    this.balance = balance;

  }

}

​

 

Second, the source code:

D39_construction_method

Account.java

address:

https://github.com/ruigege66/Java/blob/master/D39_construction_method

https://github.com/ruigege66/Java/blob/master/Account.java

2.CSDN: https: //blog.csdn.net/weixin_44630050 (Xi Jun Jun Moods do not know - Rui)

3. Park blog: https: //www.cnblogs.com/ruigege0000/

4. Welcomes the focus on micro-channel public number: Fourier transform public personal number, only for learning exchanges, backstage reply "gifts" to get big data learning materials

 

 

Guess you like

Origin www.cnblogs.com/ruigege0000/p/11668996.html