javascript
Brief description  about Online courses   join in Online courses
View Akhmad Durdibayevich Qutlimuratov 's Profile

ABOUT CLASS DECLARATION

Hi teacher, in the following class declaration,

public class StudentRegister{
private String name;
private String surname;
private int age;

public CustomerDetails(String firstname, String lastname, int new_age){
name = firstname;
surname = lastname;
age = new_age;

}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getSurname() {
return surname;
}

public void setSurname(String surname) {
this.surname = surname;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

}

there are private fields, constructor, and getter and setters as well. My question is that, if there are getters and setters in a class, do we really need constructors? I think no.
Thanks
Replies (2)
View omer abdekgadr alzwain 's Profile
constructor gurantee initializaion of fields when creating an object of the class, while you may forget to use setter methods to initialize fields.
Dec 27, 2010
View arun kumar das 's Profile
Hi

Constructor is not a mandatory in case of getter and setter, constructor is using for initialize the object.

java teacher
Dec 13, 2010