Java Construction methods

Source: Internet
Author: User

Construction Method

First, construction method considerations
1. The name of the construction method must be the same as the name of the class I am in, even the case.
2. Construction method do not write the return value type, not even void.
3. The construction method cannot return a specific return value.
4. If no construction method is written, the compiler will default to a constructor method, without parameters, and the method body does nothing.
5. Once you have written at least one of the construction methods, the compiler will no longer be presented.
6. The construction method can also be overloaded.

The purpose of the construction method is to facilitate the assignment of the properties of the object, you can skip setter statements, but, later want to change the attributes, or the setter method cannot escape.

Second, the format of the method of construction

1  Public class name (parameter list) {2//dosomething3 }


Iii. How to assign values to member variables

Method One: Object name. member Variable name = new value
Deprecated, because after learning the encapsulation, all member variables are decorated with private, which is not available.

Way two: by setter method
Student stu = new Student ();
Stu.setname ("Zhang San");

Way three: Through the method of constructing a parameter.
Student stu = new Student ("Zhang San", 30);

四、一个 Standard classes should have those things

  1  public   testa{  2  //    3   4  //   5   6      7   8  //   9  10  // getter method and Setter method  11  12 } 

Five, quick to generate a standard class skills

In idea, you can select Code->generate directly in the title bar or press the shortcut key Alt+insert directly.

+constructor Generation Construction Method
+getter & Setter Generation GetXXX and Setxxx methods

Such a standard class can be generated quickly.

Java Construction methods

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.