Subscribe Our Channel

header ads

Search This Blog

Friday, July 28, 2023

Java program to demonstrate example of this keyword. | JAVA LANGUAGE | JAVA PROGRAMMING

This program will demonstrate use of this keyword, in this program we will see what will happen if we do not use this keyword even actual and formal arguments of the methods are same and what will happen if we will use this.


//Java program to demonstrate use of this keyword


public class ExThis {

    private String name;

    private int age;

    private float weight;


    //without using this keywords

    public void getDetailsWithoutThis(String name, int age, float weight) {

        name = name;

        age = age;

        weight = weight;

    }


    //using this keywords

    public void getDetailsWithThis(String name, int age, float weight) {

        this.name = name;

        this.age = age;

        this.weight = weight;

    }


    public void putDetails() {

        System.out.println("Name: " + name);

        System.out.println("Age: " + age);

        System.out.println("Weight: " + weight);

    }


    public static void main(String args[]) {

        //Object creation

        ExThis objExThis = new ExThis();


        objExThis.getDetailsWithoutThis("Mr. Neel", 25, 78.5 f);

        System.out.println("Values after get details using getDetailsWithoutThis():");

        objExThis.putDetails();


        objExThis.getDetailsWithThis("Mr. Neel", 25, 78.5 f);

        System.out.println("Values after get details using getDetailsWithThis():");

        objExThis.putDetails();

    }

}

No comments:

Post a Comment

Featured Post

Happy Republic Day Wish Using HTML and CSS | 26 January wish using HTML/CSS

 Happy Republic Day Wish Using HTML and CSS, 26 January wish using HTML/CSS <!-- WELCOME ALL OF YOU ON COMPUTER SOFT SKILLS CHANNEL -----...

Popular Posts