Home  >  Article  >  Java  >  What are the similarities and differences between constructor method and set method in Java?

What are the similarities and differences between constructor method and set method in Java?

王林
王林forward
2020-07-21 17:14:054153browse

What are the similarities and differences between constructor method and set method in Java?

Analysis of similarities and differences:

(Recommended tutorial: java introductory tutorial)

Similarities:

Both assign values ​​to member variables.

Differences:

1. Timing of calling

The construction method is automatically called by the jvm when the object is created; the set method is manually called after the object is created.

(Video tutorial recommendation: java video tutorial)

2. The main body of the call

The construction method is automatically called by jvm; the set method is an object The name is called manually.

3. Number of calls

The constructor can only be automatically called once by the jvm when the object is initialized; the set method can be called multiple times.

The above is the detailed content of What are the similarities and differences between constructor method and set method in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete