0

I don't know how to phrase my question any better in the small space I had. Here's a better explanation:

I have a String[] test with a length of 5. The class is used to edit test to be editable in another class. Anyways my constructor passes in all 5 strings to be set to test. I originally had it set up like so:

public Generic(String field1, String field2, String field3, String field4, String field5){
test[0] = field1;
test[1] = field2;
test[2] = field3;
test[3] = field4;
test[5] = field5;
}

But I found I wanted to use the methods I have in the class to set them instead. So instead of test[0] = field1; I use setField1(field1);, etc. All I have in setField1(String Field) is the same thing that used to be in the constructor.

I just wanted to make sure that this isn't a dumb move to make, calling methods in my class in the constructor to set a variable. Or if I shouldn't employ that habit. Or my thinking is fine.

Thanks!

Eric
  • 373
  • 1
  • 3
  • 14

4 Answers4

1

The only reason to use a setter in this scenario is if the setter has validation logic. Otherwise, you're just adding ever so slight overhead to the JVM by calling a method that is otherwise identical to this.x = x;.

Mike Thomsen
  • 36,828
  • 10
  • 60
  • 83
  • Gracias thought there was something I wouldn't know. Though how much overhead are we talking about? Current app isn't going to be using much by means of memory. – Eric Mar 11 '14 at 20:25
  • Overhead is so minimal, that it is negligible, IMO. Maybe in the future you want logic code in one of your setters. Therefore, I always call the setter function, instead of setting it directly. – Nathan Merrill Mar 11 '14 at 20:30
1

My first time answering a question, so here goes nothing. The phrasing of your question was difficult to understand btw.

You should use setter methods in case you need to change only one item in an array. Also, your constructor should probably utilize a loop rather than setting each one individually in case your array size is altered in the future.

Also, if you're attempting to edit these values (after I'm assuming they've been set once, constructor or otherwise) from another class as if you've stated, you'll need to use the setters if the variable is set to private.

Serge
  • 608
  • 4
  • 12
  • 24
  • Oh I apologize for the wording. I already have getters and setters for my variables (really just the `String[]` but multiple points on that string array). I was merely curious if it was good practice to set the variables with a setter rather than directly doing so. – Eric Mar 11 '14 at 20:37
0

Generally speaking calling methods in your constructor to initialize variables is a good practice. It would be even better if those methods are not used except in the constructor to make a private method that does the work and just call this one method in the constructor or split it up to several private functions if they are not going to be used outside that class. So I would have something like the following:

public Generic(String field1, String field2, String field3, String field4, String field5) {
    initialize_fields(field1, field2, field3, field4, field5);
}

private void initialize_fields(String field1, String field2, String field3, String field4, String field5) {
    test[0] = field1;
    test[1] = field2;
    test[2] = field3;
    test[3] = field4;
    test[4] = field5;
}

This is following encapsulation that you make the methods that are not used except within the class private (You hide the details of your class and provide a simple interface for the user of your class to follow).

Another side is that it's probably a good idea to use an array as input to the constructor rather than 5 values and use a loop to assign the values to the test array.

Using setters is also almost always a good idea if you need to check on a condition before setting a value in the setter method, meaning you have validations or conditions that have to be checked in the setter method it is not just assigning. Also you have to use them if this is not used inside the class otherwise it doesn't matter and it's your preference to go for what you want meaning you can use direct assigning or setters as you wanted to do.

Thresh
  • 470
  • 6
  • 18
  • Unfortunetly I'm forced to use a constructor with five values as I'm 'getting' those values from user input. And I'm also forced to make the methods public for use in other classes. – Eric Mar 11 '14 at 20:41
  • I've gone ahead and encapsulated my variable setter thanks for the information. Seem to be getting several different answers though, some saying I shouldn't call methods in the constructor that are overideable. – Eric Mar 11 '14 at 20:52
  • You can take the input from the user and insert it in an array of course you would know better I don't know the whole story, but generally in this case I would do something like the following: `String[]input = new String[5];for(int i = 0; i < 5; i++) { input[i] = scanner.next(); } and then `new Generic(input);` assuming you made the constructor to take an array. – Thresh Mar 12 '14 at 10:13
  • I agree that calling a method that's overridden in subclasses is a bad practice because it is hard to debug if another programmer takes over your code or need to use it for any reason. The idea is that the behaviour of the object will be difficult to trace and it's not readable. One more thing is that the whole point of organizing your code and OOP is to make the code readable, maintainable, and easy to understand for other programmers that's the main motive behind any technique or strategy in coding. – Thresh Mar 12 '14 at 10:21
0

Using methods to initialize your variables isn't a bad thing by itself, but you shouldn't call methods that could be overridden from your constructor.

See this answer for more detail: Why is it considered bad practice to call a method from within a constructor?

Community
  • 1
  • 1
Mike B
  • 5,390
  • 2
  • 23
  • 45
  • I'm not using this as a setter, I'm editing a String Array with the constructor. I encapsulated it now though, I don't call set methods in any other class. – Eric Mar 11 '14 at 20:50
  • As long as the methods you call from your constructor can't be overridden then you should be good. – Mike B Mar 11 '14 at 20:51