DEV Community

Infanta Mano
Infanta Mano

Posted on

Constructor

package dynamic;

public class Construct
{
String model;
int year;

public Construct(){
    this.model = "camry";
    this.year = 2020;
}
public Construct(String model){
    this.model = model;
    this.year = 2025;
}
public Construct(String model,int year){
    this.model = model;
    this.year = year;
}


public static void main (String[] args)
{
    Construct myCar1 = new Construct();
    System.out.println(myCar1.model);
    System.out.println(myCar1.year);

    Construct myCar2 = new Construct("Carnival");
    System.out.println(myCar2.model);
    System.out.println(myCar2.year);

    Construct myCar = new Construct("Fusion", 2020);
    System.out.println(myCar.model);
    System.out.println(myCar.year);
Enter fullscreen mode Exit fullscreen mode

}

}

Top comments (0)