Constructors for Java Basics Java

Source: Internet
Author: User

The Java constructor is used to create an instance of the class and is an important way to create an object, so the Java class must contain one or more constructors

 when we do not provide any constructors for the class, the system automatically provides a parameterless constructor for the class, and when we provide one or more constructors for the class, the system does not automatically create the parameterless constructor, and if we want to use the parameterless constructor, we will create one ourselves.  one of the greatest uses of the constructor is to specify the initial value for the object's properties when the object is createdExample code:
 Packageobject; Public classCar {//If you do not create a constructor, the system automatically creates an parameterless constructor as follows//at this point, the object property is assigned a default value     PublicCar () {}//and create a constructor with one parameter.     PublicCar (String carname) { This. Setcarname (Carname); }        //another constructor with one parameter     PublicCar (String cartype,string carname) { This. Cartype =Cartype;  This. Carname =Carname; }        /*The above two constructors call different, a call to the external set method, a call to the property * in the test page, the use of all the constructors created by the object, can output the incoming value, * But, since the class properties are defined as private, That means you do not want to be directly acquired by the outside world, * so as not to pass the actual value ...     Therefore, it is better to add a judgment, or call the external set method. * */        //defining the properties of a car    PrivateString Carname; PrivateString Cartype; //Get/set methods for external delivery     PublicString Getcarname () {returnCarname; }     Public voidsetcarname (String carname) { This. Carname =Carname; }     PublicString Getcartype () {returnCartype; }     Public voidSetcartype (String cartype) { This. Cartype =Cartype; }}
 Packageobject; Public classTestcar { Public Static voidMain (string[] args) {//No referenceCar car1 =NewCar (); //because the modifier of the property in car is private//so only the value of the property is assigned by the Get/set methodSystem.out.println (Car1.getcarname ()); //with a parameter This.set methodCar car2 =NewCar ("Mustang");                System.out.println (Car2.getcarname ()); //with two parameters this. PropertyCar CAR3 =NewCar ("Ford", "Mustang");        System.out.println (Car3.getcartype ());            System.out.println (Car3.getcarname ()); }}    

overloading of constructors:

in the same class, more than one constructor, the constructor's formal parameter list is different, called the constructor reload. You can initialize a Java object based on a different constructor.  The appropriate constructor can be called in the constructor with the This keywordExample code:
1  Packageobject;2 3  Public classTrain {4     5     /*overloading of Constructors*/6      PublicTrain () {}7     8      PublicTrain (String type) {9          This. Type =type;Ten     } One      A      PublicTrain (String type,string color) { -         //This invokes the corresponding constructor -          This(type); the          This. color =color; -     } -      -      PublicTrain (String type,string color,string longth) { +         //This invokes the corresponding constructor -          This(type, color); +          This. Longth =longth; A     } at      -     //Properties -      PublicString type;//type -      PublicString color;//Color -       PublicString longth;//length -       in      //test Method -       Public voidTraininfo (Train t) { toStringBuffer SB =NewStringBuffer ("This is a column"); +          if(T.longth! =NULL&& t.longth.length () >0){ -Sb.append ("by" +t.longth+ "car"); the          } *          if(T.type! =NULL&& t.type.length () >0){ $Sb.append (t.type+ "composed of");Panax Notoginseng          } -          if(T.color! =NULL&& t.color.length () >0){ theSb.append ("Color is" +t.color+ "); +          } ASb.append ("Train"); the           + System.out.println (sb.tostring ()); -      } $       $      //Main function -       Public Static voidMain (string[] args) { -          the          //create objects based on different constructors -Train T1 =NewTrain ();Wuyi           theTrain t2 =NewTrain ("22B type passenger car")); -           WuTrain t3 =NewTrain ("25T bus", "blue-white"); -           AboutTrain T4 =NewTrain ("25T type passenger car", "green", "16"); $           - t1.traininfo (t1); -          //This is a train . - T2.traininfo (T2); A          //This is a train of type 22B bus + T3.traininfo (T3); the          //This is a row of the 25T bus color is a blue-white train - t4.traininfo (T4); $          //This is a list of 16 cars of the 25T model of the color is a green train the     } the}
Train.java

Constructors for Java Basics Java

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.