Five Important Keywords USE OOP in Java

This Keyword

⇒ This keyword in java :-

This keywords is basically a refrence variable in java. But this keyword also has a lot of usage in java.This is used to refers the current object.

Usage of This keyword:-

• This Keyword can be used to refer current class instance variable.

• This keyword can be used to invoke current class method(implicitly)

• This Keyword can be used invoke current class constructor.

• This Keyword can be used to pass as an argument in method call.

•This Keyword can be used to pass as an argument in constructor call.

• This Keyword can be used to return the current class instance from method.

Without this Keyword

class Test1

{

int i;

void setValues(int i)

{

i=i;

}

void show()

{

System.out.print(i);

}

}

class Test2

{

public static void main(String[] args)

{

Test1 T= new Test1();

T.setValues(10);

T.show();

}

}

⇒ Output :0

This program is showing output zero because name of instance and local variable is same which is “i”.In setValues() method used in class Test1 both “i=i” will be considered as local variables so when we call show() in Test2 class then Instance variable of “Test1” class will not get the values from the argument passes in setValues method.

For this problem “this” keyword comes into play which refers the value of “i” of setValues() method to instance variable “i” in the example which is given below.

With this Keyword

class Test1

{

int i;

void setValues(int i)

{

this.i=i;

}

void show()

{

System.out.print(i);

}

}

class Test2

{

public static void main(String[] args)

{

Test1 T= new Test1();

T.setValues();

T.show();

}

}

⇒ Output :10

Here “this.i” will pass the value to instance variable i. And then show() method is called it gives output 10.

⇒This Keyword can be used to invoke current class method (implicitly)

class ThisDemo

{

void display()

{

System.out.print(“hello”);

}

void show()

{

this.display();

}

}

class Test2

{

public static void main(String[] args)

{

ThisDemo td= new ThisDemo();                  

td.show();

}

}

⇒ Output :hello

In the above program if you don’t use the this keyword compiler automatically adds this keyword while invoking the method.

⇒ This Keyword can be used invoke current class constructor {this()}.

class ThisDemo

{

ThisDemo()

{

System.out.print(“no argument constructor”);

}

ThisDemo(int a)

{

this();

System.out.print(“parameterised constructor”);

}

          public static void main(String[] args)

{

ThisDemo td= new ThisDemo(10);                  

}

}

⇒ Output :

no argument constructor

parameterised constructor

⇒ This Keyword can be used to pass as an argument in method call

class ThisDemo

{

void m1(ThisDemo td)

{

System.out.print(“I m in m1 method”);

}

void m2

{

m1.(this);

}

public static void main(String[] args)

{

ThisDemo td= new ThisDemo();        

td.m2();            

}

}

⇒ Output :I m in m1 method

⇒ This Keyword can be used to pass as an argument in constructor call.

class Test

{

Test(ThisDemo td)

{

System.out.print(“test class constructor”);

}

}

class ThisDemo

{

void m1();

{

Test t=new Test(this);

}

public static void main(String[] args)

{

ThisDemo t= new ThisDemo(); 

t.m1();              

}

}

⇒ Output :test class constructor

⇒ This Keyword can be used to return the current class instance from method

class ThisDemo

{

ThisDemo m1()

{

return this;

}

public static void main(String[] args)

{

ThisDemo t= new ThisDemo(); 

t.m1();              

}

}

Super keyword in Java

⇒ Definition :- The Super keyword is a reference variable which is used to refer immediate parent class object.Means by using super keyword we can access the parent class objects from child class.Super keyword is used with the concept of inheritance in java.

Program for the Encapsulation

class A

{

int i=10;

}

class B extends A

{

void show(int i)

{

System.out.println(i);

System.out.println(super.i);

}

public static void main(String[] args)

{

B obj = new B();

obj.show(20);

}

}

Output:-

20

10

⇒Uses of ‘Super’ keyword

1. “Super” keyword can be used to refer the immediate parent class instance variable.

2. “Super” keyword can be used to invoke immediate parent class methods.

3. “Super()”can be used to invoke immediate parent class constructor.

1.super keyword to refer parent class instance variables

class A

{

int i=10;

}

class B extends A

{

void show(int i)

{

System.out.println(i);

System.out.println(super.i);

}

public static void main(String[] args)

{

B obj = new B();

obj.show(20);

}

}

Output:-

20

10

2. super keyword to invoke parent class methods

class A

{

void m1()

{

System.out.println(” I am in class A”);

}

}

class B extends A

{

void show()

{

System.out.println(“I am in class B”);

super.m1();

}

public static void main(String[] args)

{

B obj = new B();

obj.show();

}

}

Output:-

I am in class B

I am in class A

3.Super() to invoke parent class constructor.

class A

{

A()

{

System.out.println(” I am A class constructor”)

}

}

class B extends A

{

B()

{

super();

System.out.println(“I am B class constructor”);

}

public static void main(String[] args)

{

B obj = new B();

}

}

Output:-

I am A class constructor

I am B class constructor

Final keyword in Java

⇒ Definition :- Super The keyword is used in java to put restrictions on the entities like variable,methods etc.

Final keyword can be used in different context such as:

• Final Variable

• Final Method

• Final Class

⇒ Final Variable:-

When we declare any variable in java with keyword Final then we can’t modify the value of that variable in further coding.Then that value in the variable called as the constant variable.Final variable with no value is called as the blank final variable.For example pie=3.14 having constant value and gravity g =9.8 m/s consider as the universal constant value.For this type of values we use final variables in java.

Program for the Final Variable

class Test

{

public static void main(String[] args)

{

final int i=10;

i=i+20;// compile-time error

System.out.println(i);

}

}

Output:-cannot assign a value to final variable i

⇒Final Method :- When we make any method final then we can’t override it.If we want that no one can override our method then we can make it final.

Program for the Final method

class Demo

{

final void m1()

{

System.out.println(“I am in class demo”);

}

}

class Test extends Demo

{

void m1()

{

System.out.println(“I am in class Test”);

}

public static void main(String[] args)

{

Demo d = new Demo();

d.m1();

}

}

Output:-Compile-time error

Static Keyword

⇒Static variables in java :-

A variable having “Static” keyword in front of it is called static variables in java. It is also called non-access modifier.Static variable can be accesses directly through class. Static variable belong to class not an object. Static variable gets memory only once in the class area at the time of class loading. Static variable occupies space in class or method area.

Usage of static keyword:-Static keyword can be used with class level variable.
Static keyword can be used with Method.
Static keyword can be used with block.
Static keyword can be used in inner class.
Static keyword is used for memory management.

class Employee

{

int empid;

String name;

static String Company=”Smart Programming”

Employee(int empid, String name)

          {

this.empid=empid;

this.name=name;

}

void display()

{

System.out.println(empid+” “+name+” “+Company);

}

public static void main(String args[])

{

Employee e1=new Employee(101,”Amit”);

e1.display();

Employee e2=new Employee(102,”Deepak”);

e2.display();

}

}

⇒ Output :

101 Sabi Smart Programming 

102 Guru Smart Programming

Static Method

⇒Static Method in java :-

Static method belong to class not object.Static method can be called directly by class name.

Rules for Static method:-

Static method can be called without use of class name but only within class scope.
A static method can access only static data. It cannot acces non-static data(Instance data)
A static method can call only other static methods and cannot call a non-static method.
A static method cannot refer to this and super keyword in anyway.
Static method is also used for memory management.


Example of static method

class Test

{

static void display()

{

System.out.println(“1”);

}

public static void main(String args[])

{

Test.display();

}

⇒ Output :1.A static method can access only static data. It cannot acces non-static data(Instance data)

class StaticDemo

{

static int i=10;

static void display()

{

System.out.println(i);

}

}

  • A static method can call only other static methods and cannot call a non-static method

class StaticDemo

{

static int i=10;

static void display()

{

show();

System.out.println(“1”);

}

static void show()

{

System.out.println(“2”);

}

}

Static block in Java

⇒ Definition :- When we use Static keyword with any block that is called as the static block.

Declaration of static block:-

class StaticBlockDemo

{

staic

{

System.out.println(“hello”);

}

public static void main(String[] args)

{

System.out.println(“Iam in main method”);

}

}

Important points about static block

•We can’t call the static blocks.

• static blocks executes automatically when the class is loaded in memory.

• A class can have multiple static blocks.

• static blocks are executed in same sequence in which they have been written into the program.

• We can execute static blocks without main method but only with jdk 1.6 and lesser versions.

• Code inside the static block executed only for once.

• Also static blocks are executed before constructor.

Program for the static block.

class StaticBlockDemo

{

static

{

System.out.println(“I am static block 1”);

}

static

{

System.out.println(“I am static block 2”);

}

public static void main(String[] args)

{

System.out.println(“I am main method”);

}

}

Output:-

I am static block 1

I am static block 2

I am main method

Leave a Comment