Setter Methods or Constructors

Setter methods or constructors

You should use the constructor approach, when you want to create a new instance of the object, with the values already populated(a ready to use object with value populated). This way you need not explicitly call the setter methods for each field in the object to populate them.

You set the value using a setter approach, when you want to change the value of a field, after the object has been created.

For example:-

MyObject obj1 = new MyObject("setSomeStringInMyObject"); // Constructor approach
// Yippy, I can just use my obj1, as the values are already populated
// But even after this I can change the value
obj1.setSomeString("IWantANewValue"); // Value changed using setter, if required.
..
MyObject obj2 = new MyObject();
obj2.setSomeString("setSomeStringNow"); // Setter approach
// values weren't populated - I had to do that. Sad :(

And as Axel mentioned, if you want to create immutable objects, you cannot use setter-methods approach. I won't say everything has to be initialised in the constructor because different approaches exist, like lazy-evaluation which can be used even with immutable objects.

Should I use getters and setters in constructors?

You should not call getters and setters from the constructor.

A constructor constructs the specific class in which it is defined. It is its job to initialise the fields because - well - nothing else will.

The only way to guarantee initialising the fields is to assign them. If you call a setter there is a chance it could be overridden and it might do something else. It might call a method in a sub-class which is not initialised yet.

Calling a getter is also a bad idea if you are just getting a field from the same class. If it has been declared in the super-class you might justify it; if you need to get data from the super-class in the sub-class, you will have to call the getter (unless it is protected). If you need to communicate data from a sub-class to the super-class during construction you should pass it as a parameter. But this is a different use-case to what you are describing and the sub-class would probably not have your own field corresponding to the getter anyway.

If you have any "special" initialisation code, put that in a separate private method and call it from both the constructor and the setter separately.

Difference between constructor and getter and setter

I tell you an easy way:

getters() and setters():

  • actually getters() and setters available in POJO/Bean classes.
  • The main reason used for getters() setters() in java classes is To get the Java Encapsulation Mechanism.
  • In POJO/Bean classes we declare all attributes as a private. that means these class attributes can't use in other classes and packages, so in this, we can achieve Encapsulation.

Constructors():

  • I think you know the definition of constructor, The constructor is used for initializing the attributes giving our own values rather than storing the default values
  • We can say another way i.e Constructor used for creating an object and setters used for changing the values inside object, getters() user for getting the values, this is only the main difference.

What is the difference between setter methods and constructor methods?

Constructors construct new object, where as setters are created to update that object.

So lets say your name is Ding Dong and you live at, and you create a person object with your name and following address.

12345 15th Street
Area 51, Nevada 12345

and you move couple of blocks down the street to:

1234 15th Street
Area 51, Nevada 12345

Well you do not need to create new Person object because you moved, you just need to update address. That is where you will use setter method.

Conclusion:
Setters are there to update your record and constructors are there to create new Person object.

I hope this helps!!!!

Constructor and Getter- and Setter Methods

You actually can do that just make sure your getters and setters are public,an easy way to ensure your getters and setters are correct is to right click in your class->source->generate getters and setters then choose which attributes you want to be read and write,
However if your intention
with this line

    User admin = new User(username, fName, nName, password, 1);

is to create a new admin with these attributes

username = "admin";
fName = "name";
nName = "secondName";
password = "password";
group = 1;

maybe try instead:

either passing the values to the constructor directly without the variables

      User admin = new User ("admin","name","secondName",password,1);

or if you want for these to be the default values that an admin object is to have every time you create it then you can do this in the constructor:

     public Admin (){
super("admin","name","secondName",password,1);

then

    Admin admin = new admin();

then you can change the username like you want and if you want to check that it changed use

    System.out.println(admin.getUsername());

in both cases you don't really need to create the instance variables as they are inherited from user class.

calling setters from a constructor

Personally, I would set the variable directly in most cases.

Methods usually expect that the instance is fully-formed by the time they're called. In particular, calling an overridden method from a constructor is a recipe for hard-to-understand code and hard-to-spot bugs.

Having said that, I often try to make classes immutable anyway, in which case not only is there no setter, but you have to set the final variable from the constructor (or a variable initializer) anyway :)

Where properties have logic, setter logic is usually validation and sometimes change propagation to observers. I'd usually expect the constructor parameters to be checked explicitly at the start of the method, and you wouldn't want any change propagation to occur before an instance is fully created anyway.



Related Topics



Leave a reply



Submit