how to use java constructor

A constructor is a special method in java that is used to instantiate the class when creating object of that class.

every java class required to have constructor.If the constructor is not provided then Java JVM will spin a default constructor.

A default constructor is constructor that does not takes in any parameters

example bellow shows a default constructor.

public class Person {

private int age;
private String name;
private String city;


//default constructor
public Person() {


}

//class constructor
public Person(int age, String name, String city) {

this.age = age;
this.name = name;
this.city = city;
}



}


If we ever wanted to create an object from the class provided above we may supply the arguments or use method called getters and setters to set values of the object.
Previous Post Next Post

Welcome, New Friend!

We're excited to have you here for the first time!

Enjoy your colorful journey with us!

Welcome Back!

Great to see you Again

If you like the content share to help someone

Thanks

Contact Form