Answers for "this keyword in java"

1

this in java

this() : used for calling the constructor . 
  we can only use it in the constructor
this. : used for calling the instance variables 
we can use in any object instances
Posted by: Guest on May-28-2021
2

this keyword in java

// java program using ‘this’ keyword
class Demo
{
   int m;
   int n;
   public void setValue(int m, int n)
   {
      // java this keyword
      this.m = m;
      this.n = n;
   }
   public void showValue()
   {
      System.out.println("Value m = " + m);
      System.out.println("Value n = " + n);
   }
}
public class ThisExample
{
   public static void main(String[] args)
   {
      Demo obj = new Demo();
      obj.setValue(5,6);
      obj.showValue();
   }
}
Posted by: Guest on November-24-2020
1

how to use the this keyword in java

class Other{
    public double num;
    public Other(int num){
        this.num = num;
        System.out.println(num);
      	//print 5 to the console
    }
}

class scratch{
    public static void main(String[] args) {
        Other method = new Other(5);
        System.out.println(method.num);
      	//prints 5.0 to the console
    }
}
Posted by: Guest on January-08-2020
0

this keyword in java

// this keyword in java example
import java.util.*;
class Demo
{
   // instance variable
   int m;
   int n;
   public void setValue(int m, int n)
   {
      m = m;
      n = n;
   }
   public void showValue()
   {
      System.out.println("Value m = " + m);
      System.out.println("Value n = " + n);
   }
}
public class ThisKeywordDemo
{
   public static void main(String[] args)
   {
      Demo obj = new Demo();
      obj.setValue(5, 6);
      obj.showValue();
   }
}
Posted by: Guest on November-24-2020

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language