0

This question is not connected to a concrete problem but is rather a general one. In Java, when creating a class, there are actually two options how to enable access to the instance variables of that class:

  1. By creating a constructor, who will have a list of arguments that will set up the values of the instance variables.

  2. By adding setters (and getters) methods to the class for setting up the instance variables.

So now I am wondering: which option is actually better to do? I have also read that it is not good approach to keep instance variables not set up when creating an object, it is also advisable to set them some value. Is that true? In that case I would see the first option as the better one.

MichalB
  • 3,281
  • 6
  • 32
  • 46
  • Depends on the situation? Generally when there are more than 1 options there isn't an approach that is best suited for all situations. – Tim Apr 02 '14 at 12:53

5 Answers5

1

Unless there is a pressing need to change values after construction, I'd strongly recommend setting the fields in the constructor. The reason is that only fields set in the constructor can be made immutable, and immutability is a very good thing, as it makes it impossible for the class to be altered, by reflection or any other means.

An example of a final field:

public class MyClass  {
   private final int id;
   public MyClass(int id)  {
      this.id = id;
   }

When a field is changeable in a setter, the only way to lock the class down is by either doing a check such as

public void setID(int id)  {
   if(id != -1)  {
      throw  new IllegalStateException("Already set!");
   }
   this.id = id;
}

Or by somehow locking the object as a whole:

public void lock()  {
   if(getID() == -1)  {
      throw  new IllegalStateException("Must first setID(i).");
   }
   isLocked = true;
}
public void setID(int id)  {
   if(isLocked())  {
      throw  new IllegalStateException("isLocked() is true.");
   }
   this.id = id;
}

As should be obvious, setting fields in the constructor is a whole lot less complicated.

Community
  • 1
  • 1
aliteralmind
  • 19,847
  • 17
  • 77
  • 108
0

Put them in the constructor if you think they're somehow related to the object creation. For example creating a square, you'll need it's size, but adding a color is not a mandatory thing So in this case length will be set in the constructor, color will be set via setter method.

olqmin
  • 145
  • 6
0

It all depends on software design and intent, is there a use case for creating a new object without having all the parameters? Some of the parameters? Getters/Setters and other internal methods should all do check on input/data prior to performing any work - this ensures that whatever assumptions you may have made are checked prior to moving forward.

Dan
  • 876
  • 5
  • 15
0

The pure object oriented idea of a constructor is to create an instance, which is in a consistent state. It is the responsibility of the constructor to ensure, no instance can be created which is not consistent. Then, each method of that instance has the responsibility to transfer the instance from a consistent state into another consistent state.

Hence, I would always suggest to enforce setting the instance variables in the constructor where you have to validate all input parameters.

If some of the instance variables are allowed to be modified, provide a setter. To make thinks consistent, use that setter in your constructor as well! To prevent constructor pollution, you should have at least one constructor which takes all mandatory attribute values for which you can't provide any reasonable defaults or for which you won't like to provide a setter, because their values are final after construction.

Harmlezz
  • 7,972
  • 27
  • 35
0

For me use Constructor Approach:

If you want object Ready to Use.

Setters for further changes in Object.

Deepu--Java
  • 3,742
  • 3
  • 19
  • 30