How to define getter and setter in Java

in #utopian-io8 years ago (edited)

What Will I Learn?

  • How to define getter and setter methods

Difficulty

  • Intermediate

Tutorial Contents

imagesource

Under normal conditions it is not possible to reach a private element in another class. In Java there is a way to access these private elements. In C #, these are called get-set methods. In Java, these are called getter and setter methods.

With these methods private members can be read or their values can be changed. Getter method to read, setter method to write.

The getter methods do not take parameters because they read. Setter methods take parameters because they assign values to private members.

Now, i'm gonna show how to define getter and setter methods with an example.

public Class GetterSetter
{
    private int value; //private variables defined
    private String name;
    public int getValue () { //getter method
        return value;
    }
    public void setValue(int value) { // setter method
        this.value = value;
    }
    public Streing getName () { //getter method
        return Name;
    }
    public void setName(String name) { /setter method
        this.name = name;
    }
    public static void main(String [] args)
    {
        Getter Setter n1=new GetterSetter();
        n1.setName ("Merry");
        Sytem.out.println(n1.getName() );
        n1.setValue(5);
        System.out.println(n1.getValue() );
    }
}

On the screen we'll see is;

Merry

5

  1. We create an object in the main method.
  2. We call setName () method on this object and we give Merry as parameter. With this, we assign the parameter Merry to the private variable "name".
  3. We print this variable on the screen we assigned with n1.getName (). This metod gives us the "name" variable because it has the "return name;" line.
  4. We then call the setValue () method on this object and assign it a value of 5 as a parameter. So we assign 5 parameters to the private variable "value".
  5. Finally, we call the n1.getValue () method. And this method returns us the "value" variable because it has the "return value;" line.

We can give "getter" methods the name we want. But starting with the "get" progression will increase the readability of the code. In this way, we could read the values of "private" elements and write over them.

We do not only access "private" elements with getter and setter methods. In the constructor of a class we can also assign values to "private" elements. But we can not read the value. Because the builders do not return value. In addition, another method may be called within the constructors.

Now i'll teach the same example with builders.

public class GetterSetter
{
    private int value; // private areas
    private String name;

    public int getValue() {
        return value;
    }
    public void setValue(int value) {
        this.value = value;
    }
    public String getName() {
        return name;
    }
    public void setName(String name1){ 
        this.name = name1;
    }
    public GetterSetter(String name1, int number) // constructive
    {
        setName(name1); // setter method called
        setValue(number); // setter method called
    }
    public static void main(String [] args)
    {
        GetterSetter n1=new GetterSetter("Susan",10);
        System.out.println(n1.getName ());
        System.out.println(n1.getValue ());
    }
}

On the screen we'll see is;

Susan

10

While we were building the object on the first line in the "main" method, we sent Susan and 10 values to our constructor. In the constructor these values are sent to "setter" methods. So instead of calling "setter" methods in "main", we could assign the values directly to "private" variables. The line below is for us to use for this;

name=name1;
value=number;



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Your contribution cannot be approved because it does not follow the Utopian Rules.

  • Tutorials must be technical instructions that teach non-trivial aspects of an Open Source project.

Explanation

  • Your tutorial is just about simple use of the funciton that can be considered as somethin trivial

You can contact us on Discord.
[utopian-moderator]

Coin Marketplace

STEEM 0.04
TRX 0.32
JST 0.075
BTC 64465.86
ETH 1681.46
USDT 1.00
SBD 0.42