Java constructor and common function Usage Details, java constructor details

Source: Internet
Author: User

Java constructor and common function Usage Details, java constructor details

A function is also called a method!

Functions:

1. Define features and encapsulate them.

2. code reuse can be improved.

Function considerations:

1. You cannot apply a function (you cannot define a function in a function ).

2. A function can be executed only when it is called.

3. Basic data types (String, int ,....) The type of the modified function, which must have the return value.

4. For void-modified functions, the return Statement in the function can be omitted without writing.

5. The function name can be specified as needed.

Code example: (whether there are differences between functions and methods)

Example of code without functions/methods:

Public class NoFunc {public static void main (String [] args) {// main is also a function for running the program int a = 1; int B = 2; int addSum = 0; int mulSum = 0; addSum = a + B; mulSum = a * B; System. out. println ("addition" + addSum); System. out. println ("multiplication" + mulSum); a = 2; // modify the value of a, and perform the addSum = a + B; mulSum = a * B; System. out. println ("addition" + addSum); System. out. println ("multiplication" + mulSum );}}

Example code of common functions/methods:

Public class Func {int a = 1; // a is the actual parameter int B = 2; void Cal (int addSum, int mulSum) {// sum is the formal parameter addSum = a + B; mulSum = a * B; System. out. println ("addition" + addSum); System. out. println ("multiplication" + mulSum); // void has no return value} // modify the value of a, and perform the int setA (int a) {// a as the form parameter this. a = a; // The actual parameter is assigned to the form parameter return a; // return value a} public static void main (String [] args) {// main is also a function, used to run the program Func f = new Func (); // create the object f. cal (); // calls the Add function for an object. 0 is assigned to sum (initialization) f. setA (2); // a value is 2f. cal (0, 0); // perform another operation }}

Running result: (Same)

Addition 3
Multiplication 2
Addition 4
Multiplication 4

Function category:

1. Common functions

2. Constructor

3. main Function (special)

Constructor considerations:

1. The method name of the constructor must be the same as the class name.

2. The function type cannot be declared, there is no return type, and it cannot be defined as void.

3. There cannot be any non-access modifier modifiers, such as static, final, synchronized, and abstract.

4. the constructor cannot be called directly. It must be called using the new keyword.

Role of constructor:

1. convenient parameter transfer.

2. Call the constructor to initialize the object. It is the initialization of objects that match the given format (parameter list.

Example of constructor code:

Public class Constructor {int a = 233; int B = 233; Constructor () {// No parameter Constructor} Constructor (int a, int B) {// constructor with parameters this. a = a; this. B = B;} void Cal () {int addSum = a + B; int mulSum = a * B; System. out. println ("addition" + addSum); System. out. println ("multiplication" + mulSum); // void has no return value} // modify the value of a, and perform the int setA (int a) {// a as the form parameter this. a = a; // The actual parameter is assigned to the form parameter return a; // return value a} public static void main (String [] args) {Constructor c1 = new Constructor (); // The object c1.Cal () created by the Constructor without parameters; // The Constructor c2 = new Constructor (1, 2) is called by the Constructor object; // The object initializes c2.Cal (); // a constructor object calls the Cal function c2.setA (2); // a value is 2 c2.Cal (); // another operation }}

Running result:

Addition 466
Multiplication 54289
Addition 3
Multiplication 2
Addition 4
Multiplication 4

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.