4

I have an object in which the constructor's job is to set the fields and they aren't set up or changed after that, ever. However, I need a getter for them. Then, do I need to have setters even though I won't ever use them "just in case" or I can remove them and leave the getters only while leaving setting the values to the constructor?

Straightfw
  • 2,143
  • 5
  • 26
  • 39
  • 4
    Don't write code that's not needed. Why would you anyway? It's just a waste of time and more potential bugs. – Mat Jan 20 '13 at 19:11
  • I see. Thank you and sorry for the silly question :) – Straightfw Jan 20 '13 at 19:13
  • The ability to leave out setters is pretty much the whole point. You're restricting certain operations (i.e. writing) where needed. That's encapsulation. – Gigi Jan 20 '13 at 19:24
  • 2
    By default, you should strive to make your objects [immutable (final fields, no setters)](http://stackoverflow.com/questions/279507/what-is-meant-by-immutable). If that is not possible, then you can start to relax that rule. But unless you have to comply with some external specification, having setters should certainly not be the first thing to do. – assylias Jan 20 '13 at 19:53
  • See also this interesting discussion: http://stackoverflow.com/questions/3511120/why-shouldnt-i-use-immutable-pojos-instead-of-javabeans, especially the emphasis on correctness (it is easier to reason about a state that does not change) and thread safety. – assylias Jan 20 '13 at 19:54

6 Answers6

3

According to the java bean specification, it is legal to omit a getter or setter method. so, you can safely omit the setter if you never use them

PermGenError
  • 45,977
  • 8
  • 87
  • 106
2

If your constructor is setting the fields and you don't need to change them they sound like they are immutable.

I would make them final and then just have getters.

If you omit the final then you allow someone in the future to add setters. If you don't need setters then you should enforce this as part of your design and have gettets with final fields.

This will make it explicit to anyone reading the code that they are immutable by design.

There is also the issue of writing more code than us needed. If you write setters then you could introduce bugs as this code may not be fully tested as you don't consider it needed

RNJ
  • 15,272
  • 18
  • 86
  • 131
0

If you want to use the object property as read-only and initialize it in the object's constructor then you don't need the setter. If you want to access this property outside of this class, then you need getter for that.

Also suggest you to read this SO Question

Community
  • 1
  • 1
evilone
  • 22,410
  • 7
  • 80
  • 107
0

below is from an architect at google :

class Foo{

   private int a;
   private int b;

   public Foo(int num1, int num2){
      a= num1;
      b= num2;
   }

   public int getA(){
     return a;
   }

   public int getB(){
     return b;
   }

}

Here you dont need setter and once you construct your object. you cant change the state of it.

DarthVader
  • 52,984
  • 76
  • 209
  • 300
-1

In the case of something that only you will see it's all personal choice since most editors can add them automatically for you.

If it is something that should never ever change and bad things can happen if it does, then that should not have a setter and should be a private variable.

If you are writing something with an eye towards re-use and sharing with others, then you do want getters and setters anywhere it is ok to do so since you won't know what other people may need to do with your API.

Erik Nedwidek
  • 6,134
  • 1
  • 25
  • 25
-1

I think it depends on what your constructor is doing. If your constructor is merely doing raw setting of variables, then I don't think you need to include setters for them.

However, if your constructor code is doing any kind of business logic prior to setting the value of a variable, then I think this warrants creating a setter for at least the variable in question (if not all of them).

For example, if your constructor code does this, then don't include any setters:

public MyClass( String varA, String varB, int varC )
{
  this.varA = varA;
  this.varB = varB;
  this.varC = varC;
}

But if your code does this, you should include a setter to decouple the logic and make it cleaner:

public MyClass( String varA, String varB, int varC )
{
  if ( varA == null )
  {
    this.varA = '(empty)';
  }
  else
  {
    this.varA = varA;
  }

  this.varB = varB;

  if ( varC < 0 )
  {
    callSomeMethod();
  }

  this.varC = varC;
}

This kind of logic warrants creating setters.

mightyrick
  • 910
  • 4
  • 6