2

I've had often the case that an API defines a class which only consists of its fields with the appropriated setters and getters.

However, they have had a specific role. So from a real life (OOP) point of view they actually were meaningful. The last time I've stumbled about this was the schema in Olingo. It's used to set a few properties.

My question is, is there any advantage over "just setting variables" from a technical point of view or are these classes only used to stick to OOP (and have clean code and so on)?

Edit: Please note that I'm not asking why we are using "Setters" and "Getters". Try to look at it from another perspective. Let's say you have to define three Strings to use them further in your code. Instead of defining them as "on the fly" private Strings, you decide to create a class storing these three strings as fields and defining setters and getters for them. Is there any technical advantage to do so?

Sample code for "schema":

public List<Schema> getSchemas() throws ODataException {
List<Schema> schemas = new ArrayList<Schema>();

Schema schema = new Schema();
schema.setNamespace(NAMESPACE);

List<EntityType> entityTypes = new ArrayList<EntityType>();
entityTypes.add(getEntityType(ENTITY_TYPE_1_1));
entityTypes.add(getEntityType(ENTITY_TYPE_1_2));
schema.setEntityTypes(entityTypes);

List<ComplexType> complexTypes = new ArrayList<ComplexType>();
complexTypes.add(getComplexType(COMPLEX_TYPE));
schema.setComplexTypes(complexTypes);

List<Association> associations = new ArrayList<Association>();
associations.add(getAssociation(ASSOCIATION_CAR_MANUFACTURER));
schema.setAssociations(associations);

List<EntityContainer> entityContainers = new ArrayList<EntityContainer>();
EntityContainer entityContainer = new EntityContainer();
entityContainer.setName(ENTITY_CONTAINER).setDefaultEntityContainer(true);

List<EntitySet> entitySets = new ArrayList<EntitySet>();
entitySets.add(getEntitySet(ENTITY_CONTAINER, ENTITY_SET_NAME_CARS));
entitySets.add(getEntitySet(ENTITY_CONTAINER, ENTITY_SET_NAME_MANUFACTURERS));
entityContainer.setEntitySets(entitySets);

List<AssociationSet> associationSets = new ArrayList<AssociationSet>();
associationSets.add(getAssociationSet(ENTITY_CONTAINER, ASSOCIATION_CAR_MANUFACTURER, ENTITY_SET_NAME_MANUFACTURERS, ROLE_1_2));
entityContainer.setAssociationSets(associationSets);

entityContainers.add(entityContainer);
schema.setEntityContainers(entityContainers);

schemas.add(schema);

return schemas;
}

Added an example which contains exactly the content I'm questioning. Consider the class "test" as a class which contains two fields "a" and "b" and the appropriated "setters" and "getters". Simple example:

public class Main {
    public static void main(String[] args) {

        //Version 1: Common practice

        test asdf = new test();
        asdf.setA("asdf");
        asdf.setB("asdf2");

        //Doing something with "asdf" and "asdf2"

        //Version 2: My request

        String a = "asdf";
        String b = "asdf2";

        //Doing something with "asdf" and "asdf2"

    }
}
emmanuel
  • 9,607
  • 10
  • 25
  • 38
OddDev
  • 3,644
  • 5
  • 30
  • 53
  • Well you can't have an Interface on top of fields, only methods, so that is a big advantage, plus there is the whole Java Bean auto wiring thing. But I think this entire concept is covered in depth by some commonly sites blog post/paper that I'm sure someone has at the tip of mind :) – Jason Sperske May 04 '15 at 09:10
  • yes, if you actually change the setter method to do some extra work, or check if the new value of a variable is in range. imagine you would have to do it and you would change the variable to be private instead of public, and make a setter method to implements these extra tasks before or after setting or getting a variable. You would need to change the whole code which directly assigns a variable. – SomeJavaGuy May 04 '15 at 09:11
  • Setters are great for debugging. It will tell you exactly when a variable changes value. You don't need to set breakpoints all over the code. – cup May 04 '15 at 09:15
  • It allows use of proxies, and eases mocking in tests – Serge Ballesta May 04 '15 at 09:18
  • Re your edit: If the strings are only used in the method, I've never seen your "common practice" in the wild. – T.J. Crowder May 04 '15 at 09:52

4 Answers4

3

There are lots of real-world practical advantages to getters/setters:

  • If you need to add logic to them (usually a setter), you can do so without breaking your API.

  • When debugging, if you need to know when a field is changed, you can set a breakpoint in the setter.

  • You can use an interface to define your API.

  • Subclasses can add logic to them.

  • If appropriate, the exposed type of the getter/setter can be a more generic or limited version of the actual field being used (for instance, a getter can be a read-only List), allowing you to change the implementation (perhaps an ArrayList becomes a LinkedList) without, again, breaking your API.

  • They can be proxied for testing.

In theory, the real-world, practical disadvantage is that you're making method calls rather than just setting fields. But if it's important from a performance standpoint, the JVM's just-in-time optimizing compiler will inline simple getters/setters.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • I think that the OP wants to know about advantages of *Olingo* over the conventional getters and setters – Blip May 04 '15 at 09:23
  • Made an edit: Please note that I'm not asking why we are using "Setters" and "Getters". Try to look at it from another perspective. Let's say you have to define three Strings to use them further in your code. Instead of defining them as "on the fly" private Strings, you decide to create a class storing these three strings as fields and defining setters and getters for them. Is there any technical advantage to do so? – OddDev May 04 '15 at 09:48
  • Note: point 1 is less relevant within a single project, since your IDE probably has a "change to getter and setter" operation. – user253751 May 08 '15 at 00:11
  • I still got one unrelated question: How are those classes called? I love using them because they're really neat to use and allow easy data serialization, but I have no clue what I should enter in google if i got a question about them. I hope someone is able to help me out. Thanks – BlueWizard Jun 11 '15 at 10:30
  • @JonasDralle: What, classes with getters and setters? – T.J. Crowder Jun 11 '15 at 11:29
  • @T.J.Crowder Yes, how are they called? :3 – BlueWizard Jun 11 '15 at 11:54
  • 1
    @JonasDralle: I don't think there's a general term for them. – T.J. Crowder Jun 11 '15 at 12:25
0

Definitely there are advantage.

With this setter and getter you allowing other to access you property when you monitoring.

Which I mean, You are giving a controlled access. You can filter and check the value that other object setting to the fields.

Saif
  • 6,804
  • 8
  • 40
  • 61
0

It is useful to have methods to access and set the attributes (consistency check, encapsulation...), but it is boilerplate having to do it explicitly when the getter/setter has the default action.

Many languages now use myObject.attr as the only syntax, be it for attributes or getters. Default getters and setters are automatically (and silently) generated if the attributes are visible. Scala is one of those languages and to me, that's the best solution.

Dici
  • 25,226
  • 7
  • 41
  • 82
0

With Setters , we can abstract away the logic for setting the variable.

For Ex : Minimum Balance can't be negative. If we use the variable directly to set the balance, we may set it to negative value by mistake. This can be avoided with the setters.

public Class Account {

    public int minBalance;

    public void setMinBalance(int amount) {
        if(amount < 0) minBalance = 0;
        else minBalance = amount;
    }
}