Monday, 15 May 2017

Tutorial 7: Getters and Setters. What Do These Mean?


Setters

A setter is simply a function that sets a value for you.
In Java, we normally declare setters as public functions that can access the private member variables of a class’s objects and set their values.

A setter normally has a parameter and its return type is void, meaning that it returns nothing after it executes.
For example:

Let us define a Person class with two member variables: an age and a name. Age is of type int while name is of type String. Then, we create a setter for the age variable and another setter for the name variable.

public class Person {

            private int age;

            private String name;



            public void setAge(int age) {

                        this.age = age;

            }

           

            public void setName() {

                        this.name = name;

            }

           

public static void main (String[] args) {

            Person person1 = new Person();

            person1.setAge(15);

            person1.setName(“Jacob”);

            }

}

Getters

A getter is simply a function that gets a value for you.
In Java, we normally declare getters as public functions that can access the private member variables of a class’s objects and retrieve their values.

A getter does not have any parameters and its return type is similar to the datatype of the parameter whose value it gets.
For example:

Let us define a Person class with two member variables: an age and a name. Age is of type int while name is of type String. Then, we create a getter for the age variable and another getter for the name variable.

public class Person {

            private int age;

            private String name;



            public int getAge() {

                        return age;

            }

           

            public String getName() {

                        return name;

            }

           

public static void main (String[] args) {

            Person person1 = new Person();

            System.out.println(“Person1’s name is ” + person1.getName());

            System.out.println(“Person1’s age is ” + person1.getAge());

            }

}

Now that we know how to define getters and setters, let us put everything together into one class.

A getter is simply a functions that gets a value for you.
In Java, we normally declare getters as public functions that can access the private member variables of a class and retrieve their values.

A getter does not have any parameters and its return type is similar to the datatype of the parameter whose value it gets.
For example:

Let us define a Person class with two member variables: an age and a name. Age is of type int while name is of type String. Then, we create a getter for the age variable and another getter for the name variable.

public class Person {

            private int age;

            private String name;

           

public void setAge(int age) {

                        this.age = age;

            }

           

            public void setName(String name) {

                        this.name = name;

            }



            public int getAge() {

                        return age;

            }

           

            public String getName() {

                        return name;

            }

           

public static void main (String[] args) {

            Person person1 = new Person();

            person1.setAge(15);

            person1.setName(“Jacob”);

            System.out.println(“Person1’s name is ” + person1.getName());

            System.out.println(“Person1’s age is ” + person1.getAge());

            }

}


Figure 1: Getters and Setters. You can even use a getter and setter from another class, within the same package or after importing another package.


It is important that I mention that most IDEs can generate getter and setter functions for you. You have to first define your member variables. In Eclipse, you can right-click within the body of the file containing the file, go to Source and select Generate Getters and Setters.


Figure 2: How to generate getters and setters in Eclipse IDE.


So, practice creating your getters and setters, try using them from another class within the same package and from different packages and see what happens. Also, try to get your IDE to generate getters and setters for you and see whether or not they look identical to what we’ve discussed above.


If you have any questions or comments, please drop them in the comments section below and I will address them as soon as possible.

In the next tutorial, we talk about the static key word and how it is used. But until then, stay safe and practice hard.

No comments:

Post a Comment