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:

myObject.name = "Tom";

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

package setters;

public class SleepTime {

int timev;
int time;

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

public int getSleepTime() {
return time;


  .inpostComics img {
    max-width: 250px;

  .inpostComics {
      padding: 25px;
      margin-bottom: 10px;
      border-radius: 7px;
      max-width: 900px;

  .inpostComics .comicLink a {
    font-size: 14px;
    font-family: Arial;
  .textLink {
    font-size: 13px;
    font-family: arial;
    margin-bottom: 18px;
  .comicLink {
    max-width: 260px;

  .inpostComics .comicLink {
      margin-bottom: 20px;
  /* this part adds an icon after external links, using FontAwesome */

.inpostComics a[href^="http://"]:after,
.inpostComics a[href^="https://"]:after,
.sidebarComics a[href^="http://"]:after,
.sidebarComics a[href^="https://"]:after
  content: "\f08e";
  font-family: FontAwesome;
  font-weight: normal;
  font-style: normal;
  display: inline-block;
  text-decoration: none;
  padding-left: 3px;

.inpostComics a[href].noCrossRef::after,
.sidebarComics a[href].noCrossRef::after
    content: ""!important;
    padding-left: 0;
<div class="container overflow-hidden">
<div class="row inpostComics gy-4">
<h3>Sponsored content</h3>
        var contents=new Array()

        contents[0]='<div class="border col-md-4"><a class="noCrossRef" ref="nofollow" href="https://idbwrtng.com/paligoinline_apr24_2022"><img style="border: 1px solid darkgray" src="https://s3.us-west-1.wasabisys.com/idbwmedia.com/images/paligojune.png" alt="Paligo" /></a><div class="comicLink"> <a href="https://idbwrtng.com/paligoinline_apr24_2022">Find out why Paligo is the best solution for publishing your API docs</a></div></div>'

        contents[1]='<div class="border col-md-4"><a class="noCrossRef" ref="nofollow" href="https://idbwrtng.com/devdocs"><img style="border: 1px solid darkgray" src="https://s3.us-west-1.wasabisys.com/idbwmedia.com/images/devdocs_square.jpg" alt="DevDocs: Your tech writing project partner" /></a><div class="comicLink"> <a href="https://idbwrtng.com/devdocs">DevDocs is your tech writing project partner for all your doc needs</a></div></div>'

        contents[2]='<div class="border col-md-4"><a class="noCrossRef" ref="nofollow" href="https://idbwrtng.com/acrolinx_inline"><img style="border: 1px solid lightgray" src="https://s3.us-west-1.wasabisys.com/idbwmedia.com/images/acrolinx_square.png" alt="Acrolinx" /></a> <div class="comicLink"><a href="https://idbwrtng.com/acrolinx_inline">Acrolinx for technical communication: Communication at a new level</a></div></div>'

        contents[3]='<div class="border col-md-4"><a class="noCrossRef" ref="nofollow" href="https://idbwrtng.com/archbeeinline_apr24_2022"><img style="border: 1px solid lightgray" src="https://s3.us-west-1.wasabisys.com/idbwmedia.com/images/archbeesquare.jpeg" alt="Archbee: The Knowledge Base, rebuilt for software companies" /></a> <div class="comicLink"> <a href="https://idbwrtng.com/archbeeinline_apr24_2022">Archbee makes it easy to build, launch, and manage your developer portal</a></div></div>'

        contents[4]='<div class="border col-md-4"><a class="noCrossRef" ref="nofollow" href="https://idbwrtng.com/xpublisherinline_apr24_2022"><img style="border: 1px solid lightgray" src="https://s3.us-west-1.wasabisys.com/idbwmedia.com/images/xeditor2022.png" alt="Explore Xeditor, a web-based & highly customizable XML editor for any CMS" /></a> <div class="comicLink"> <a href="https://idbwrtng.com/xpublisherinline_apr24_2022">Explore Xeditor, a web-based & highly customizable XML editor for any CMS</a></div></div>'

        contents[5]='<div class="border col-md-4"><a class="noCrossRef" ref="nofollow" href="https://idbwrtng.com/blobrinline_apr24_2022"><img style="border: 1px solid lightgray" src="https://s3.us-west-1.wasabisys.com/idbwmedia.com/images/blobr_red.png" alt="Blobr: The portal to document, distribute and monetize your APIs as products" /></a> <div class="comicLink"> <a href="https://idbwrtng.com/blobrinline_apr24_2022">The portal to document, distribute and monetize your APIs as products</a></div></div>'

        //Your ad here
        //contents[5]='<div class="border col-md-4"><a class="noCrossRef" ref="nofollow" href="https://idbwrtng.com/youradhereinline"><img style="border: 1px solid darkgray" src="https://s3.us-west-1.wasabisys.com/idbwmedia.com/images/youradhere2.png" alt="Advertising options for tech comm products" /></a><div class="comicLink"> <a href="https://idbwrtng.com/youradhereinline">Make your tech comm products visible to tech writers worldwide</a></div></div>'

        var i=0
        //variable used to contain controlled random number
        var random
        //while all of array elements haven't been cycled thru
        while (i<contents.length){
            //generate random num between 0 and arraylength-1
            //if element hasn't been marked as "selected"
            if (contents[random]!="selected"){
                //mark element as selected


<div style="clear:both;"></div>

<iframe class="mj-w-res-iframe" frameborder="0" height="300px"  scrolling="no" marginheight="0" marginwidth="0" src="https://app.mailjet.com/widget/iframe/8NM8/OT7" width="100%"></iframe>

<script type="text/javascript" src="https://app.mailjet.com/statics/js/iframeResizer.min.js"></script>

and to set and retrieve them as follows:

package setters;

public class App {

public static void main(String[] args) {
SleepTime tomSleepTime = new SleepTime();



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:

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:

Robot myRobot = new Robot();

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

myRobot.id = "some value";

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


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