This keyword
It is refer to the object that is currently executed.
It initializes the value of a variable.
Syntax = this.variable name;
Example
class xyz
{
int x;
int y;
xyz(int x, int y)
{
// if instance variable and parameter variable are different than no need //of this keyword.
this.x=x;
this.y=y;
}
}
class demo
{
public static void main(String args[])
{
xyz ob = new xyz(1,2);
System.out.println(ob.x);
System.out.println(ob.y);
}
}
Output: 1
2
Static keyword
Normally any of the class members can be accessed by using object of its class, but it is possible
to create a member that can be used by itself without reference to a specific instance.
It is possible by a static keyword.
When member is declared static, it can be accessed before any objects of its class are created and
without reference to any objects.
You can declare both method and variable to be static.
Example = main.
If methods are declared static then
 They can call other static methods.
 They must only access static data.
 They cannot refer to this or super keyword in any way.
It provides static block which execute first.
Example:
class demo
{
static int a=10;
static int b;
// static variables
static void printAll(int n)
{
System.out.println("N = "+n);
System.out.println("A = "+a);
System.out.println("B = "+b);
}
// static block initialise static data
static
{
System.out.println("Static block executed");
b=a*2;
}
}
class demo
{
public static void main(String args[])
{
// static methods are directly called without object
printAll(5);
}
}
Output: N = 5
A = 10
Static block executed
B = 20
Example – 2
class teststatic
{
static int a = 10;
static int b = 20;
// static variables
static void printA()
{
System.out.println("A = "+a);
}
}
class staticinotherclass
{
public static void main(String args[])
{
// static methods are directly called without object
teststatic.printA();
System.out.println("B from other class = "+teststatic.b);
}
}
Output: A = 10
B from other class = 20
Super keyword
Sometime we may wish to use superclass constructor as they were implemented or we may wish
to refer super class variable into subclass where variables with the same name as in superclass
exists then java provides a keyword super to solve above difficulties.
You may use super keyword for
 To call superclass constructor into a subclass
 To refer superclass variable
To call superclass constructor into a subclass
Example:
class box
{
int width;
int height;
int depth;
// used when no parameter given
box()
{
width=height=depth=10;
}
box(int a)
{
width=height=depth=a;
}
box(int w, int h, int d)
{
width=w;
height=h;
depth=d;
}
// method to calculate volume of box
int volumeofbox
{
return(width*depth*height);
}
}
class boxprise extends box
{
int prise;
boxprise(int w, int h, int d, int p)
{
super(w,h,d);
prise = p;
}
}
class staticinotherclass
{
public static void main(String args[])
{
box b1 = new box();
System.out.println("Box1 volume is = "+(b1.volumeofbox));
box b2 = new box(20);
System.out.println("Box2 volume is = "+(b2.volumeofbox));
boxprise b3 = new boxprise(3,4,5,600);
System.out.println("Price of box is = "+(b3.priseofbox));
System.out.println("Box3 volume is = "+(b3.volumeofbox));
}
}
Output: Box1 volume is = 1000
Box2 volume is = 6000
Price of box is = 600
Box3 volume is = 60
When we use super to call superclass constructor then super(); must be in the first line of
subclass.
To refer superclass variable
Example:
class A
{
int a;
}
class B extends A
{
int a; int b;
B(int x, int y, int z)
{
super.a=x;
// assigns value to a variable of super class
a=y;
b=z;
}
void displayAll()
{
System.out.println("super.a =", +super.a );
// access variable of super class in sub class
System.out.println("A = ",+a);
System.out.println("B = ",+b);
}
}
class demo
{
public static void main(String args[])
{
B b1 = new B(10,20,30);
b1.displayAll();
}
}
Output: super.a = 10
A = 20
B = 30
It is most applicable to situation in which member name of subclass hide members by the same
name in the superclass.
Final keyword
 To declared as constant
Whenever you want to declare any variable whose value cannot be changed at any time then you
can do this by declaring that variable as final.
Using final you can define constant in java program
Syntax = final datatype variablename = value;
Example = final float PI = 3.14;
Generally variable declared as final which are written in uppercase.
 To prevent method overriding
Write the final precede the method than it cannot be overridden.
Method declared as final cannot be declared as an abstract method because it cannot be
overridden.
Example:
class xyz
{
final void printmessage()
{
System.out.println("hello");
}
}
class B extends xyz
{
final void printmessage()
{
// error in the following line because method declared as final cannot be override
System.out.println("hello");
}
}
 To prevent inheritance
To do this write final precede the class declaration than it can not be inherited.
Declaring class as final, indirectly declared all of its method as final too.
It is illegal to declare a class as both abstract and final.
Example:
final class A
{
}
class B extends A // illegal
{
}
A final class must full defined its methods with complete definition because they are directly
final and final method cannot be redefine.

Keyword of java

  • 1.
    This keyword It isrefer to the object that is currently executed. It initializes the value of a variable. Syntax = this.variable name; Example class xyz { int x; int y; xyz(int x, int y) { // if instance variable and parameter variable are different than no need //of this keyword. this.x=x; this.y=y; } } class demo { public static void main(String args[]) { xyz ob = new xyz(1,2); System.out.println(ob.x); System.out.println(ob.y); } } Output: 1 2
  • 2.
    Static keyword Normally anyof the class members can be accessed by using object of its class, but it is possible to create a member that can be used by itself without reference to a specific instance. It is possible by a static keyword. When member is declared static, it can be accessed before any objects of its class are created and without reference to any objects. You can declare both method and variable to be static. Example = main. If methods are declared static then  They can call other static methods.  They must only access static data.  They cannot refer to this or super keyword in any way. It provides static block which execute first. Example: class demo { static int a=10; static int b; // static variables static void printAll(int n) { System.out.println("N = "+n); System.out.println("A = "+a); System.out.println("B = "+b); } // static block initialise static data static { System.out.println("Static block executed"); b=a*2; } }
  • 3.
    class demo { public staticvoid main(String args[]) { // static methods are directly called without object printAll(5); } } Output: N = 5 A = 10 Static block executed B = 20 Example – 2 class teststatic { static int a = 10; static int b = 20; // static variables static void printA() { System.out.println("A = "+a); } } class staticinotherclass { public static void main(String args[]) { // static methods are directly called without object teststatic.printA(); System.out.println("B from other class = "+teststatic.b); } } Output: A = 10 B from other class = 20
  • 4.
    Super keyword Sometime wemay wish to use superclass constructor as they were implemented or we may wish to refer super class variable into subclass where variables with the same name as in superclass exists then java provides a keyword super to solve above difficulties. You may use super keyword for  To call superclass constructor into a subclass  To refer superclass variable To call superclass constructor into a subclass Example: class box { int width; int height; int depth; // used when no parameter given box() { width=height=depth=10; } box(int a) { width=height=depth=a; } box(int w, int h, int d) { width=w; height=h; depth=d; } // method to calculate volume of box int volumeofbox {
  • 5.
    return(width*depth*height); } } class boxprise extendsbox { int prise; boxprise(int w, int h, int d, int p) { super(w,h,d); prise = p; } } class staticinotherclass { public static void main(String args[]) { box b1 = new box(); System.out.println("Box1 volume is = "+(b1.volumeofbox)); box b2 = new box(20); System.out.println("Box2 volume is = "+(b2.volumeofbox)); boxprise b3 = new boxprise(3,4,5,600); System.out.println("Price of box is = "+(b3.priseofbox)); System.out.println("Box3 volume is = "+(b3.volumeofbox)); } } Output: Box1 volume is = 1000 Box2 volume is = 6000 Price of box is = 600 Box3 volume is = 60 When we use super to call superclass constructor then super(); must be in the first line of subclass.
  • 6.
    To refer superclassvariable Example: class A { int a; } class B extends A { int a; int b; B(int x, int y, int z) { super.a=x; // assigns value to a variable of super class a=y; b=z; } void displayAll() { System.out.println("super.a =", +super.a ); // access variable of super class in sub class System.out.println("A = ",+a); System.out.println("B = ",+b); } } class demo { public static void main(String args[]) { B b1 = new B(10,20,30); b1.displayAll(); } } Output: super.a = 10 A = 20 B = 30 It is most applicable to situation in which member name of subclass hide members by the same name in the superclass.
  • 7.
    Final keyword  Todeclared as constant Whenever you want to declare any variable whose value cannot be changed at any time then you can do this by declaring that variable as final. Using final you can define constant in java program Syntax = final datatype variablename = value; Example = final float PI = 3.14; Generally variable declared as final which are written in uppercase.  To prevent method overriding Write the final precede the method than it cannot be overridden. Method declared as final cannot be declared as an abstract method because it cannot be overridden. Example: class xyz { final void printmessage() { System.out.println("hello"); } } class B extends xyz { final void printmessage() { // error in the following line because method declared as final cannot be override
  • 8.
    System.out.println("hello"); } }  To preventinheritance To do this write final precede the class declaration than it can not be inherited. Declaring class as final, indirectly declared all of its method as final too. It is illegal to declare a class as both abstract and final. Example: final class A { } class B extends A // illegal { } A final class must full defined its methods with complete definition because they are directly final and final method cannot be redefine.