Overload, initialization block, this, default constructor

Source: Internet
Author: User
Import java. util. *;/* class loading sequence: first, execute static fields and static code blocks, then execute instance fields and common code blocks, and then execute constructors. */Public class sample {public static void main (string [] ARGs) {employee [] Staff = new employee [3]; staff [0] = new employee ("Harry ", 40000); staff [1] = new employee (60000); staff [2] = new employee (); For (employee item: Staff) {system. out. println ("name =" + item. getname () + ", id =" + item. GETID () + ", salary =" + item. getsalary () ;}} class employee {public employee (string N, double S) {This. name = N; this. sala Ry = s;} public employee (double S) {This ("Employee #" + nextid, S); // it must be the first line; otherwise, the compilation is incorrect, it cannot be used with super.} Public employee () {// If a class does not define its own constructor, the system will provide its own default constructor.} Public String getname () {return name;} public double getsalary () {return salary;} public int GETID () {return ID;} Private Static int nextid; private int ID; private string name = ""; private double salary; {// code block id = nextid; nextid ++;} static {// static code block random generator = new random (); nextid = generator. nextint (10000 );}}


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.