Instance Variable Hiding in java program – “this keyword”

What is Instance variable Hiding in Java Program?

– When our instance variables (class variables) are same with parameter variables that time have to specified our class variables so that time we use “this keyword” in our program.

– At that time we hide instance variable in “this keyword”. So we can call instance variable hiding.

 

 

Let’s see example on instance variable hiding in java program – “This keyword”

class ThisKeyword

{

            int No;                                                              // instance variable

            String name,address;

 

            void setData(int No,String name,String address)          // Parameter Variable

            {

                        this.No = No;

                        this.name = name;

                        this.address = address;

            }

            void displayData()

            {

            System.out.println(“No = ” + this.No + “\nName = ” + name + “\nAddress = ” + address);

            }

}          

class clEx4

{

            public static void main(String args[])

            {

                        ThisKeyword obj = new ThisKeyword();          // Object

 

                        obj.setData(101,”Ami Watsone”,”Gree Valley”);

                        obj.displayData();                    

            }

}

 

Output:

No = 101

Name = Ami Watsone

Address = Green Valley

 

In this example, we can see instance variables No, name and address are same as setData(int No,String name,String address) parameters variable.

This keyword” cover the instance variable and indicate as class variables.

 

Video available on YouTube.

 


[sbscrbr_form]

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


 

518total visits,5visits today


mayank

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

Related articles

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