Constructors are used to assign values to the class variables at the time of object creation, either explicitly done by the programmer or by Java itself (default constructor).

在创建对象时,可以使用构造函数将值分配给类变量,这些值可以由程序员显式完成,也可以由Java本身(默认构造函数)完成。

Constructors in Java - GeeksforGeeks

Rules for writing Constructor:

  • Constructor(s) of a class must has same name as the class name in which it resides.

    构造函数的名字和类名必须一致。

  • A constructor in Java can not be abstract, final, static and Synchronized.

    java中的构造函数不能是抽象的,最终的,静态的和同步的。

  • Access modifiers can be used in constructor declaration to control its access i.e which other class can call the constructor.

    可以在构造函数声明中使用访问修饰符来控制其访问,即哪个其他类可以调用构造函数。

构造函数的类型:

  1. 无参构造函数

    没有参数的构造函数被认为时默认构造函数。若我们没有在类中定义构造函数,那么编译器会自己定义一个无参数的构造函数。Default constructor provides the default values to the object like 0, null, etc. depending on the type.(默认的构造函数提供默认的值给对象0,null,等,更具他们的类型不同)

    // Java Program to illustrate calling a // no-argument constructor import [java.io](http://java.io).*;
    
    class Geek { int num; String name;
    
     // this would be invoked while an object 
     // of that class is created. 
     Geek() 
     { 
     	System.out.println("Constructor called"); 
     } 
    
    
    }
    
    class GFG { public static void main (String[] args) { // this would invoke default constructor. Geek geek1 = new Geek();
    
     	// Default constructor provides the default 
     	// values to the object like 0, null 
     	System.out.println(geek1.name); 
     	System.out.println(geek1.num); 
     } 
    
    
    }

0utput :

Constructor called
null
0

2. 有参构造函数:

If we want to initialize fields of the class with your own values, then use a parameterized constructor.如果我们想使用我们自己的值去给类中的变量赋值的话,那么使用参数化的构造函数(有参构造函数)。

// Java Program to illustrate calling of 
// parameterized constructor. 
import java.io.*; 

class Geek 
{ 
	// data members of the class. 
	String name; 
	int id; 

	// constructor would initialize data members 
	// with the values of passed arguments while 
	// object of that class created. 
	Geek(String name, int id) 
	{ 
		this.name = name; 
		this.id = id; 
	} 
} 

class GFG 
{ 
	public static void main (String[] args) 
	{ 
		// this would invoke the parameterized constructor. 
		Geek geek1 = new Geek("adam", 1); 
		System.out.println("GeekName :" + geek1.name + 
						" and GeekId :" + geek1.id); 
	} 
}

Output:

GeekName :adam and GeekId :1

也就是说每一个类里面都有一个构造函数用来给变量赋值。如果你自己不去写这个构造函数,那么编译器会写一个。这样就可以给你创建的所有变量默认赋值了。(如果构造函数有参数那么,就是你给定的值;如果没有参数,那么就是0或者时null)。对了,构造函数中也可以写其他的语句,可以夹带私货。

使用构造参数时的一些其他问题:

  • Does constructor return any value? (有返回值么?没有,但可以写return)

    There are no “return value” statements in constructor, but constructor returns current class instance. We can write ‘return’ inside a constructor.

  • Constructor Overloading.(我们同样将其视作函数【本来就是】,当然也可以像函数一样,通过参数的类型,数目和顺序的不同来区分不同的同名构造函数)

    Like methods, we can overload constructors for creating objects in different ways. Compiler differentiates constructors on the basis of numbers of parameters, types of the parameters and order of the parameters.

How constructors are different from methods in Java?构造函数和函数的区别

  • Constructor(s) must have the same name as the class within which it defined while it is not necessary for the method in java.构造函数的名字只能和类名相同,而且也不是必要的。
  • Constructor(s) do not return any type while method(s) have the return type or void if does not return any value.构造函数没有返回值
  • Constructor is called only once at the time of Object creation while method(s) can be called any numbers of time.构造函数只有在对象被创建的时候调用一次,而函数可以无限次被调用。