---
title: "Java: Setters"
date: 2015-01-01
description: "## Quick Summary * setters refers to a technique for encapsulating a value so that users don"
canonical_url: https://idratherbewriting.com/java-setters/
---
# Java: Setters
## Quick Summary

 - setters refers to a technique for encapsulating a value so that users don’t set invalid data types or other incorrect data as a field value.

Eclipse example: getter_and_setter_methods

Classes have instance variables, but usually users don’t need to know which variables are available in the class. Instead, it’s more common to provide methods that allow users to set the value of the instance variables.

In other words, rather than doing something like this to set the value:

```java
myObject.name = "Tom";
```

It’s more common to use a setter method as follows:

```java
package setters;

public class SleepTime {

int timev;
int time;

public int setSleepTime(int timev) {
int time = timev;
return time;
}

public int getSleepTime() {
return time;
}

}

 Sponsored

 var contents = new Array()

 //Paligo
 contents[0] = '[![Paligo](https://s3.us-west-1.wasabisys.com/idbwmedia.com/images/paligo-ad-IRBW.png)](https://idbwrtng.com/paligo_jan2026) [Smart technical documentation with easy content reuse.](https://idbwrtng.com/paligo_jan2026)'
 // 3di
 contents[1] = '[![3di - Clear technical documentation, complex translation, and content operations](https://s3.us-west-1.wasabisys.com/idbwmedia.com/images/3di.png)](https://idbwrtng.com/3di_sep25) [Clear technical documentation, complex translation, and content operations](https://idbwrtng.com/3di_sep25)'
 //Document360
 contents[2] = '[![Manage API docs and software docs all in one place](https://s3.us-west-1.wasabisys.com/idbwmedia.com/images/api_documentation_360.jpg)](https://idbwrtng.com/doc360_july2026) [Manage API docs and software docs all in one place.](https://idbwrtng.com/doc360_july2026)'

 // Ads must render at their promised 250x150. The content column is
 // ~823px, so three 250px ads only fit above 1200px (col-xl-4);
 // between 992-1199px they go two per row (col-lg-6) and stack below.
 var i = 0
 //variable used to contain controlled random number
 var random
 //while all of array elements haven't been cycled thru
 while (i 

and to set and retrieve them as follows:

package setters;

public class App {

public static void main(String[] args) {
SleepTime tomSleepTime = new SleepTime();
System.out.println(tomSleepTime.setSleepTime(11));
System.out.println(tomSleepTime.getSleepTime());

}

}
```

There’s a shortcut in Eclipse that makes it very easy to add getters and setters. Right click your class and select **Source > Generate Getters and Setters**. Here’s an example of what it produces:

```java
public class Robot {
private int id;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

}
```

Here I had just entered `private int id`. It automatically set the get and set methods for this value. It’s pretty common to set up getters and setters for field values of a class.

Note that the setter method uses `this.id = id;`. That’s because you’ll create an object from the Robot class like this:

```java
Robot myRobot = new Robot();
```

You would then set the value for the id like this:

```java
myRobot.id = "some value";
```

To make it generic, you use `this` instead. The word `this` refers to whatever the object in scope is.

## Encapsulation

> Encapsulation means that an object holds its contents in such a way that other objects can’t see or change those contents (though we have a number of ways to provide access to the contents of a class).
> Java for Absolute Beginners

 var contents = new Array()
 // One ad slot, two creatives, same as the sidebar: the new dark State of
 // Knowledge creative in contents[3] alternates with the light August one
 // in contents[1]. contents[0] is the dark August creative it replaced --
 // paused Sep 21, 2026, and expected back in ~2 months, at which point
 // swap it in for contents[3].
 // contents[0] = '