Keywords in Java

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Java Keywords

Static keyword: In Java, if we want to access class members, we must first create an instance of the
class. But there will be situations where we want to access class members without creating any
variables.

In those situations, we can use the static keyword in Java. If we want to access class members without
creating an instance of the class, we need to declare the class members static.

NOTED: IN JAVA Main Method has static.

Static Variables: in Java, when we create objects of a class, then every object will have its own copy
of all the variables of the class.

class Btech

{ static int id = 25; // static variable

int roll = 6; // non-static variable

public class CSE

{ public static void main(String[] args)\

{ Btech obj = new Btech();

System.out.println("roll is print + 1 = " + (obj.roll+ 1)); // access the non-static variable

System.out.println("id is print+ 1 = " + (Btech.id + 1)); // access the static variable

}
Static Methods: Static methods are also called class methods. It is because a static method belongs to
the class rather than the object of a class.And we can invoke static methods directly using the class
name.

class Btech

{ int multiply(int a, int b) // non-static method

{ return a * b;

static int add(int a, int b) // static method

{ return a + b;

public class CSE

{ public static void main( String[] args )

{ Btech obj = new Btech(); // create an instance of the Btech class

System.out.println(" 19 *17 = " + obj.multiply(19,17)); // call the nonstatic method

System.out.println(" 87 + 3 = " + Btech.add(87,3)); // call the static method

}
final keyword: In Java, the final keyword is used to denote constants. It can be used with variables,
methods, and classes.

Once any entity (variable, method or class) is declared final, it can be assigned only once. That is

 the final variable cannot be reinitialized with another value


 the final method cannot be overridden
 the final class cannot be extended

final Variable:In Java, we cannot change the value of a final variable.

class Btech

{ public static void main(String[] args)

{ final int AGE = 32; // create a final variable

AGE = 45; // try to change the final variable

System.out.println("Age: " + AGE);

}}

final Method :Before you learn about final methods and final classes, make sure you know about

the Java Inheritance. In Java, the final method cannot be overridden by the child class.
class Btech1st

{ public final void display() // create a final method

{ System.out.println("This is a final method.");

}}

class Btech2nd extends Btech1st

{ public final void display() // try to override final method

{ System.out.println("The final method is overridden.");

} public static void main(String[] args)

{ Btech2nd obj = new Btech2nd();

obj.display();
}

this Keyword: In Java, this keyword is used to refer to the current object inside a method or a
constructor.

class MCA

{ int id;

MCA(int id)

this.id = id;

System.out.println("this reference = " + this);

public static void main(String[] args) {

MCA obj = new MCA(3);

System.out.println("object reference = " + obj);

Use of this Keyword: There are various situations where this keyword is commonly used.

1. Using this for Ambiguity Variable Names: In Java, it is not allowed to declare two or more
variables having the same name inside a scope (class scope or method scope). However, instance
variables and parameters may have the same name.

class MCA

{ int id;

MCA(int id)

{ id = id;

public static void main(String[] args) {

MCA obj = new MCA(3);


System.out.println("object ID = " + obj.id);

Using this keyword:

class MCA

{ int id;

MCA(int id)

{ this.id = id;

public static void main(String[] args) {

MCA obj = new MCA(3);

System.out.println("object ID = " + obj.id);

}}

2. this with Getters and Setters: Another common use of this keyword is in setters and getters
methods of a class.

class MCA

{ String name;

void setName( String name ) // setter method

this.name = name;

String getName() // getter method

return this.name;

public static void main(String[] args )

{ MCA obj = new MCA();


obj.setName("MASTER OF COMPUTER SCIENCE"); // calling both method

System.out.println("obj.name: "+obj.getName());

}}

3. Using this in Constructor Overloading: While working with constructor overloading, we might
have to invoke one constructor from another constructor. In such a case, we cannot call the constructor
explicitly. Instead, we have to use this keyword.

class Btech

{ private int a, b;

private Btech(int i, int j)

{ this.a = i;

this.b = j;

private Btech(int i)

{ this(i, i);

private Btech()

{ this(0);

public String toString(){

return this.a + " + " + this.b + "i";

public static void main( String[] args )

{ Btech obj =new Btech(10,30);

Btech obj1 =new Btech(60);

Btech obj2 =new Btech();

System.out.println(obj);

System.out.println(obj1);
System.out.println(obj2);

Super Keyword: The super keyword in Java is used in subclasses to access super class members
(attributes, constructors and methods). Before we learn about the super keyword, make sure to know
about Java inheritance.

Uses of super keyword:

1. To call methods of the superclass that is overridden in the subclass.


2. To access attributes (fields) of the superclass if both superclass and subclass have attributes with
the same name.
3. To explicitly call superclass no-arg (default) or parameterized constructor from the subclass
constructor.

1. Access Overridden Methods of the superclass: if methods with the same name are defined in both
superclass and subclass, the method in the subclass overrides the method in the superclass. This is
called method overriding.
class RAM
{
public void display()
{
System.out.println("I am ram");
}
}

class SITA extends RAM


{
public void display()
{
System.out.println("I am a sita");
}

public void printMessage()


{
display();
}
}
class RAMSITA
{
public static void main(String[] args)
{
SITA obj= new SITA();
obj.printMessage();
}}
Using super keyword:
class RAM
{
public void display()
{
System.out.println("I am ram");
}
}

class SITA extends RAM


{
public void display()
{
System.out.println("I am a sita");
}

public void printMessage()


{
display();
super.display();
}
}

class RAMSITA
{
public static void main(String[] args)
{
SITA obj= new SITA();
obj.printMessage();
}
}

2. Access Attributes of the Superclass: The superclass and subclass can have attributes with the same
name. We use the super keyword to access the attribute of the superclass.
class RAM
{
protected String type="ram";
}

class SITA extends RAM


{
public String type="sita";

public void printType()


{
System.out.println("I am a " + type);
System.out.println("I am an " + super.type);
}
}

class RAMSITA
{
public static void main(String[] args)
{
SITA obj= new SITA();
obj.printType();
}
}

3. Use of super() to access superclass constructor: As we know, when an object of a class is created,
its default constructor is automatically called.
To explicitly call the superclass constructor from the subclass constructor, we use super(). It's a special
form of the super keyword.
super() can be used only inside the subclass constructor and must be the first statement.

class RAM
{ RAM()
{
System.out.println("I am an ram");
}
}

class SITA extends RAM


{ SITA()
{
super(); // calling default constructor of the superclass

System.out.println("I am a sita");
}
}

class RAMSITA {
public static void main(String[] args)
{
SITA obj = new SITA();
}}

You might also like