Keywords in Java
Keywords in Java
Keywords in Java
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.
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 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
{ return a * b;
{ return a + b;
}
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
class Btech
}}
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
}}
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;
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;
class MCA
{ int id;
MCA(int id)
{ this.id = 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;
this.name = name;
return this.name;
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;
{ this.a = i;
this.b = j;
private Btech(int i)
{ this(i, i);
private Btech()
{ this(0);
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.
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 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 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");
}
}
System.out.println("I am a sita");
}
}
class RAMSITA {
public static void main(String[] args)
{
SITA obj = new SITA();
}}