Java basic tutorial constructor and method overloading
In methods and data members, we mentioned that objects in Java are initialized when they are created. During initialization, the object's data members are assigned initial values. We can initialize it explicitly. If we do not assign an initial value to the data member, the data member will take a default initial value based on its type.
Explicit initialization requires us to determine the initial value when writing the program, which is sometimes very inconvenient. We can use constructor to initialize objects. Constructors can initialize data members and specify specific operations. These operations are performed automatically when the object is created.
Define the constructor
The constructor is a method. Like normal methods, we define constructors in the class. The constructor has the following basic characteristics:
1. The name of the constructor is the same as the name of the class
2. The constructor has no return value
We define the constructor of the Human class:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public class Test
{
public static void main(String[] args)
{
Human aPerson = new Human(160);
System.out .println(aPerson.getHeight());
}
}
class Human
{
/**
* constructor
*/
Human(int h)
{
This.height = h;
System.out.println("I'm born");
}
/**
* accessor
*/
int getHeight ()
{
return this.height;
}
int height;
}
The above program will print
I'm born 160
The constructor can receive a parameter list like a normal method. Here, the constructor Human() receives an integer as parameter. In the body of the method, we assign the integer parameter to the data member height. The constructor does two things when the object is created:
The constructor can receive a parameter list like a normal method. Here, the constructor Human() receives an integer as parameter. In the body of the method, we assign the integer parameter to the data member height. The constructor does two things when the object is created:
1. Provides an initial value for the data member this.height = h;
2. Performs specific initial operations System.out.println("I 'm born");
In this way, we can flexibly set the initial value when calling the constructor without being as constrained as display initialization.
How is the constructor called? When we create classes, we all use new Human(). In fact, we are calling the constructor of the Human class. When we do not define this method, Java will provide a blank constructor to be called when using new. But when we define a constructor, Java will call the defined constructor when creating an object. When calling, we provide a parameter of 160. You can also see from the final running results that the height of the object is indeed initialized to 160.
Priority of initialization method
In methods and data members, we can see that if we provide an explicit initial value, then the data member will adopt an explicit initial value instead of Default initial value. But if we both provide an explicit initial value and initialize the same data member in the constructor, the final initial value will be determined by the constructor. For example, in the following example:
public class Test { public static void main(String[] args) { Human aPerson = new Human(160); System.out.println(aPerson.getHeight()); } } class Human { /** * constructor */ Human(int h) { this.height = h; } /** * accessor */ int getHeight() { return this.height; } int height=170; // explicit initialization }
The running result is:
160
The final initialization value of the object is consistent with the value in the construction method. Therefore:
public class Test { public static void main(String[] args) { Human neZha = new Human(150, "shit"); System.out.println(neZha.getHeight()); } } class Human { /** * constructor 1 */ Human(int h) { this.height = h; System.out.println("I'm born"); } /** * constructor 2 */ Human(int h, String s) { this.height = h; System.out.println("Ne Zha: I'm born, " + s); } /** * accessor */ int getHeight() { return this.height; } int height; }
Ne Zha: I'm born, shit 150
public class Test { public static void main(String[] args) { Human aPerson = new Human(); aPerson.breath(10); } } class Human { /** * breath() 1 */ void breath() { System.out.println("hu...hu..."); } /** * breath() 2 */ void breath(int rep) { int i; for(i = 0; i < rep; i++) { System.out.println("lu...lu..."); } } int height; }
lu...lu... lu...lu... lu...lu... lu...lu... lu...lu... lu...lu... lu...lu... lu...lu... lu...lu... lu...lu...
Constructor features: The same name as the class, no return value
Constructor purpose: Initialization, initial operation
Method overloading: method name + parameter list-> Which method is actually called
More For articles related to multiple Java basic tutorials on constructors and method overloading, please pay attention to the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use OAuth2.0's access_token to achieve control of interface access permissions? In the application of OAuth2.0, how to ensure that the...

Discussing the hierarchical architecture in back-end development. In back-end development, hierarchical architecture is a common design pattern, usually including controller, service and dao three layers...

Questions and Answers about constant acquisition in Java Remote Debugging When using Java for remote debugging, many developers may encounter some difficult phenomena. It...

Confused with choosing Java project management tools for beginners. For those who are just beginning to learn backend development, choosing the right project management tools is crucial...

Exploring the application of ultimate consistency in distributed systems Distributed transaction processing has always been a problem in distributed system architecture. To solve the problem...

Analysis of the reason why Python script cannot be found when submitting a PyFlink job on YARN When you try to submit a PyFlink job through YARN, you may encounter...

How to dynamically configure the parameters of entity class annotations in Java During the development process, we often encounter the need to dynamically configure the annotation parameters according to different environments...

Analysis of class loading behavior of SPI mechanism when Tomcat loads Spring-Web modules. Tomcat is used to discover and use the Servle provided by Spring-Web when loading Spring-Web modules...
