Friday, March 20, 2020

Writing Classes in Java-Part 3


Keywords in Java

1] ‘this   keyword –

    There are  lot of usage of this keyword. In java, this is a reference variable that refers to the current object. Within an instance method or a constructor, this is a reference to the current object - the object whose method or constructor is being called. To refer any member of current object from instance method or constructor using this can be done.



Use of this keyword-
1.     this keyword can be used to refer current class instance variable.
2.     this() can be used to invoke current class constructor.
3.     this keyword can be used to invoke current class method (implicitly)
4.     this can be passed as an argument in the method call.
5.     this can be passed as argument in the constructor call.
6.     this keyword can also be used to return the current class instance.

The  reason for using the this keyword is because a field is shadowed by a method or constructor parameter.

Example –
1]
   public class Point {
    public int x = 0;
    public int y = 0;
        
    //constructor
    public Point(int a, int b) {
        x = a;
        y = b;
    }
}
 
Same above example can be written in easy way using this keyword -
 
public class Point {
    public int x = 0;
    public int y = 0;
        
    //constructor
    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
}
2] From within a constructor, you can also use the this keyword to call another constructor in the same class

public class Rectangle {
    private int x, y;
    private int width, height;
        
    public Rectangle() {
        this(0, 0, 1, 1);
    }
    public Rectangle(int width, int height) {
        this(0, 0, width, height);
    }
    public Rectangle(int x, int y, int width, int height) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
    }
    
}

2] ‘static’ keyword –

The static keyword in Java is used for memory management. Static keyword can be applied  with variables, methods, blocks and nested classes. The static keyword belongs to class than an instance of the class. When a variable is declared as static, then a single copy of variable is created and shared among all objects at class level. Static variables are, essentially, global variables.
The static can be:
  1. Variable (also known as a class variable)
  2. Method (also known as a class method)
  3. Block
  4. Nested class
Advantage of static keyword is, it makes program memory efficient.
Example-
class Test
{
    // static variable
    static int a = m1();
      
    // static block
    static {
        System.out.println("Inside static block");
    }
      
    // static method
    static int m1() {
        System.out.println("from m1");
        return 20;
    }
      
    // static method(main !!)
    public static void main(String[] args)
    {
       System.out.println("Value of a ="+a);
       System.out.println("from main");
    }
  
  
}

OUTPUT-
From m1
Inside static block
value of a = 20
from main

Diksha Avhad(K 3)

9 comments: