Q:

Java program to demonstrate example of this keyword

belongs to collection: Java Basic Programs

0

Java program to demonstrate example of this keyword

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

This keyword example in Java

//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.5f);
        System.out.println("Values after get details using getDetailsWithoutThis():");
        objExThis.putDetails();
         
        objExThis.getDetailsWithThis("Mr. Neel",25,78.5f);
        System.out.println("Values after get details using getDetailsWithThis():");
        objExThis.putDetails();     
    }
}

Output

    Values after get details using getDetailsWithoutThis():
    Name: null
    Age: 0
    Weight: 0.0
    Values after get details using getDetailsWithThis():
    Name: Mr. Neel
    Age: 25
    Weight: 78.5

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java Basic Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java example for while loop demonstration... >>
<< Java program to demonstrate example of enum data t...