Getter – Setter Methods in Java Program

What is Getter Setter Methods?

– Getter Setter Methods are working to get data from class and to set data in class. This is one easy process for all programmers. We can also use constructor. But getter – setter is very easy and flexible for all java developer.

 

Let’s see example on Getter Setter methods.

class Student

{

            int No;

            String name,address;

            double m1,m2,m3,total,per;

            // Setter Methods

            void setNo(int No)

            {

                        this.No = No;    // this keyword

            }

            void setName(String name)

            {

                        this.name = name;

            }

            void setAddress(String address)

            {

                        this.address = address;

            }

            void setM1(double m1)

            {

                        this.m1 = m1;

            }

            void setM2(double m2)

            {

                        this.m2 = m2;

            }

            void setM3(double m3)

            {

                        this.m3 = m3;

            }

            // Getter Methods

            int getNo()

            {

                        return this.No;

            }

            String getName()

            {

                        return this.name;

            }

            String getAddress()

            {

                        return this.address;

            }

            double getM1()

            {

                        return this.m1;

            }

            double getM2()

            {

                        return this.m2;

            }

            double getM3()

            {

                        return this.m3;

            }

 

            void calculation()

            {

                        // use of getter method

                        this.total = getM1() + getM2() + getM3();

                        this.per = this.total/3;  

            }

 

            double getTotal()

            {

                        return this.total;

            }

            double getPer()

            {

                        return this.per;

            }

 

}

class clEx5

{

            public static void main(String args[])

            {

                        Student obj = new Student();

 

                        // Set Data using Setter Methods

                        obj.setNo(10);

                        obj.setName(“Tom Voltas”);

                        obj.setAddress(“10,Green Valley”);

                        obj.setM1(78.67);

                        obj.setM2(88.12);

                        obj.setM3(98.60);

 

                        obj.calculation();           // Calculate total and per

 

                        System.out.println(“–:Details:–“);

                        // Fetch data from getter methods

                        System.out.println(“No = ” + obj.getNo());

                        System.out.println(“Name = ” + obj.getName());

                        System.out.println(“Address = ” + obj.getAddress());

                        System.out.println(“M1 = ” + obj.getM1());

                        System.out.println(“M2 = ” + obj.getM2());

                        System.out.println(“M3 = ” + obj.getM3());

                        System.out.println(“Total = ” + obj.getTotal());

                        System.out.println(“Per = ” + obj.getPer());

 

            }

}

 

Output:

-: Details :-

No = 10

Name = Tom Voltas

Address = 10,Green Valley

M1 = 78.67

M2 = 88.12

M3 = 98.60

Total = 265.39

Per = 88.463

 

This is simple example of getter and setter methods. In IDE like eclipse and netbeans have some simple steps for all getter and setter methods.

 

Video available on YouTube.

 


[sbscrbr_form]

If you have query about this blog, you can directly post comment or subscribe on website.


 

692total visits,1visits today


mayank

Hello, I am Maddy a Java blogger, trainer and developer

Related articles

2 Comments

  1. October 5, 2016 - 7:57 am

    Hello, I would like to subscribe for this weblog to take hottest updates, so where can i do
    it please help out.

    • October 6, 2016 - 9:17 am

      just write your email address on subscribe form..

Leave a Reply

Your email address will not be published. Required fields are marked *

Your comments (*)

Name (*)

Your full name please.

Email address (*)

Used for gravatar.

Website

Link back if you want.

Enter Captcha Here :*

Reload Image