Home  >  Article  >  Java  >  Java functions - similarities and differences between constructors and member functions

Java functions - similarities and differences between constructors and member functions

王林
王林forward
2019-11-30 16:37:472482browse

Java functions - similarities and differences between constructors and member functions

Similarities and differences between constructors and member functions

1. External call angle

Construction Functions can only be called when the object is created;

Member functions can only be called after the object is created and can be repeated.

Related video tutorial sharing: java video

2. Return value angle

The constructor does not need to return Value;

Member functions are optional.

3. Permission modifiers

The constructor can use any permission modifier;

The member function can also use any permission modifier.

4. Internal calls

The constructor can call member functions, but this member function is generally not treated as an object behavior;

Constructor function You can call the constructor, and this (parameter list) calls other constructors in the current class;

Constructors cannot call each other, it will recur infinitely and this(...) must be in the first sentence! Prevent value overwriting;

Member functions can call member functions, member functions cannot call constructors, but they can call new. (The constructor can have the same name as the member function!)

Recommended related articles and tutorials: Zero Basic Introduction to Java

The above is the detailed content of Java functions - similarities and differences between constructors and member functions. 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