I have confusion about use of setter and constructor about value assigning. Why we use setter to set values independently if we can do it in a single constructor.
I have tried both methods using setter and construtor passing values and found no differnce. Need reasons for the differnece between them.
//setter
public class NegaE {
String name;
int id;
public void setname(String a){
this.name=a;
}
public void setid(int b){
this.id=b;
}
public String getname(){
return name;
}
public int getid(){
return id;
}
// now through constructor
public class NegaE {
String name;
int id;
public NegaE(String a,int b){
this.id=b;
this.name=a;
}
public String getname(){
return name;
}
public int getid(){
return id;
}