1

I am unsure how to create a setter method in java.
Everything that I found on the internet either shows how to "generate" one in eclipse or doesn't include what I am looking for.

I am looking for a simple answer in plain terms, if that is possible.
I understand what a setter-method does, but am unsure about the syntax and specific code.

Thank you for your time.

JDurstberger
  • 4,127
  • 8
  • 31
  • 68
matt
  • 39
  • 2
  • 8
  • 1
    Also, if you generate it with Eclipse, just follow the same syntax to write it. It's as simple as that. – dguay Nov 19 '15 at 14:10
  • 2
    I'm assuming that you are thinking of getters and setters from a C# perspective where there is actual built in syntax for them. Java doesn't have that. In Java a getter and setter is simply a function with the signature `Object getObject()` and `void setObject(Object object)`. Otherwise they aren't any different from other functions in java,besides using the get/set naming convention. The reasoning behind the naming convention is to allow applications to easily find the accessor functions by means of reflection. – Daren Coffey Nov 19 '15 at 14:20

3 Answers3

3

I'm assuming you mean a setter method as in set an object?

private String yourString;

public void setYourString(String yourString) {
    this.yourString = yourString;
}

This is basic code though so you probably mean something else?

Let me know.

vguzzi
  • 2,420
  • 2
  • 15
  • 19
2

A small code for getter and setter

public class Test {

    String s;

    public String getS() {
        return s;
    }

    public void setS(String s) {
        this.s = s;
    }

}

Advantage of setter is that a setter can do sanity checks and throw IllegalArgumentException.

RockAndRoll
  • 2,247
  • 2
  • 16
  • 35
2

A setter is a method which sets a value for one of your parameters. E.g. many people say it's not nice to have a public variable in a class:

public class SomeClass {
     public int someInt = 0;
}

now you can access the variable someInt directly:

SomeClass foo = new SomeClass();
foo.someInt = 13;

You should rather do something like that:

public class SomeClass {
    private int someInt = 0;

    // Getter
    public int getSomeInt() {
        return someInt;
    }

    // Setter
    public void setSomeInt(int someIntNew) {
        someInt = someIntNew;
    }
}

and access it through:

SomeClass foo = new SomeClass();
foo.setSomeInt(13);

All this is just convention... You could name your setter-method however you want! But getters and setters are a good (and readable) way to define access to your class varaibles as you like it (if you want to make it read-only you could just write the getter, if you don't wan't anybody to read the value you could only write the setter, you could make them protected, etc...)

ParkerHalo
  • 4,341
  • 9
  • 29
  • 51
  • Thank you! This is exactly what I was looking for. It is simple and clear. I apologize for the late response but I decided to take a little break and work on another program in the hope to get a fresh look on this one when I returned. – matt Dec 03 '15 at 19:16
  • you're welcome mate! I'm glad i could help! – ParkerHalo Dec 03 '15 at 20:54