Java Language Keywords – Top 4 Keyword in Java (Part 2)

1. Java Language Keywords

In our Java tutorial, we have already discussed the part 1 for keywords. Today, we are going to study the second part of the topic ‘Java Language Keywords’. In this part, we will study abstract, volatile, super, static, and transient keywords. Moreover, we will look at Abstract keyword, Abstract Classes, Syntax and important rules for Java Abstract keyword. In addition, we will also look at Super Keyword and its example. After that, we are going to discuss Static Keyword which contains Static blocks, Static variables, Static method and nested class. At last, we will discuss Volatile Keyword in Java.

So, let’s begin with Java Language Keywords.

Java Language Keywords - Top 4 Keyword in Java (Part 2)

Java Language Keywords – Top 4 Keyword in Java (Part 2)

2. What is Abstract Keyword in Java?

In Java Language Keywords, an abstract keyword defines that it is a non-access modifier which is applicable for classes and methods but it is not applicable for variables. These Java language keywords used to achieve abstraction in Java which happens to be a key feature of OOP.

Let us study different contexts in detail.

a. Abstract class in Java

The class which doesn’t have its all method declared or has a partial implementation is known as Abstract class.

Syntax –

abstract class class-name
{
   //body of class
}

Due to the partial implementation we usually cannot instantiate the abstract classes, i.e. all the subclass of an abstract class should either execute the greater part of the abstract methods in the super-class or announce abstract itself. Some of the predefined classes in java are abstract. They rely upon their sub-classes to give finish usage. For instance, java.lang. A number is a dynamic class.

Sometimes, we require just declaration of the method in super-classes. This can accomplish by indicating the abstract sort modifier. These techniques are infrequently alluded to as subclasses responsibilities since they have no usage indicated in the super-class. In this way, a subclass must override them to give method definition.

Do you know the difference Between Abstract Class and Interface in Java

Syntax –

abstract type method-name(parameter-list);

Important rules –
Any class that contains at least one abstract methods should likewise be called abstract.

  • final
  • Java abstract native
  • abstract synchronized
  • abstract static
  • Java abstract private
  • abstract strictfp

The accompanying is different illegal combinations of different modifiers for strategies regarding abstract modifier.

abstract class A
{
   abstract void m1();
   void m2()
   {
       System.out.println("This is a concrete method.");
   }
}
class B extends A
{
   void m1() {
       System.out.println("B's implementation of m2.");
   }
}
public class AbstractDemo
{
   public static void main(String args[])
   {
       B b = new B();
       b.m1();
       b.m2();
   }
}

Output –

B’s implementation of m2.

This is a concrete method.

Learn Java Abstract Data Type in Data Structure – ADT

3. Super Keyword

Next is Super Keyword in Java language Keywords. The super keyword inside java is a means a variable that is used to indicate parent class objects. The keyword “super” came onto the scene with the concept of Inheritance. It is majorly used in the contexts mentioned below:

  • Use of super with variables: This thing occurs when a derived class and base class carry the same data members. In that case, there might a possibility of ambiguity for the JVM. It can understand more clearly using the code snippet:
class Vehicle
{
   int maxSpeed = 120;
}
class Car extends Vehicle
{
   int maxSpeed = 180;
   void display()
   {
       System.out.println("Maximum Speed: " + super.maxSpeed);
   }
}
class Test
{
   public static void main(String[] args)
   {
       Car small = new Car();
       small.display();
   }
}

In the example above, both base class and subclass have a member maxSpeed in common. We could access maxSpeed of the base class in sublcass using super keyword.

  • Use with methods – It is done when we need to call the method of the parent class. To avoid ambiguity in classes with the same name, we use this  Super keyword.

Explore Constructor Chaining in Java – Changing Order & Using Super () Keyword

class Person
{
   void message()
   {
       System.out.println("This is person class");
   }
}
class Student extends Person
{
   void message()
   {
       System.out.println("This is student class");
   }
   void display()
   {
       message();
       super.message();
   }
}
class Test
{
   public static void main(String args[])
   {
       Student s = new Student();
       s.display();
   }
}

Output:
This is student class
This is person class

class Person
{
   Person()
   {
       System.out.println("Person class Constructor");
   }
}
class Student extends Person
{
   Student()
   {
       super();
       System.out.println("Student class Constructor");
   }
}
class Test
{
   public static void main(String[] args)
   {
       Student s = new Student();
   }
}

Output:

Person class Constructor.

Student class Constructor.

Let’s discuss Deadlock in Java – How to Detect & Reduce Java Deadlock

4. Static Keyword

Java Static is a non-access modifier which is appropriate for accompanying:

  • Blocks
  • Variables
  • Methods
  • Nested classes

To make a static member(block, variable, method, nested class), go before its revelation with the keyword static. At the point when a member is proclaimed static, it get to before any objects of its class are made, and without reference to any object. For instance, in beneath java program, we are getting too static technique m1() without making any protest of Test class.

Read Static Binding Vs Dynamic Binding in Java

class Test
{
   static void m1()
   {
       System.out.println("from m1");
   }
   public static void main(String[] args)
   {
          m1();
   }
}

Output –
From m1

Important Java Language Keywords- Static Java

Important Java Language Keywords- Static Java

a. Static Blocks

Instantiate a static block for computation. It gets executed only once and the class gets loaded.

class Test
{
   static int a = 10;
   static int b;
   static {
       System.out.println("Static block initialized.");
       b = a * 4;
   }
   public static void main(String[] args)
   {
      System.out.println("from main");
      System.out.println("Value of a : "+a);
      System.out.println("Value of b : "+b);
   }
}

Output:

Static block initialized.
from main
Value of a: 10
Value of b: 40

b. Static Variable

At the point when a variable is proclaimed as static, at that point a single copy of the variable is made and shared among all objects at the class level. Static factors are worldwide factors. All cases of the class share a similar static variable.

Important points –

  • We can make static variables at class-level as it were.
  • Static block and static variables execute all together they are available in a program.

Let’s know about What is Static Methods in Java

c. Static Methods

At the point, when a method announces with static watchword, it is known as static technique. The most well-known case of a static technique is primary( ) method. As talked about over, any static part get to before any objects of its class are made, and without reference to any object. Methods pronounced as static have a few limitations:

  • They can just straightforwardly call other static methods.
  • They can just specifically get the static information.
class Test
{
   static int a = m1();
   static {
       System.out.println("Inside static block");
   }
   static int m1() {
       System.out.println("from m1");
       return 20;
   }
   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
Learn Java String, Methods And Constructor – Syntax and Example

class Test
{
   static int a = 10;
   int b = 20;
   static void m1()
   {
       a = 20;
       System.out.println("from m1");
        b = 10;
        m2();
        System.out.println(super.a);
   }
   void m2()
   {
       System.out.println("from m2");
   }
   public static void main(String[] args)
   {
   }
}
Java Quiz

5. Volatile Keyword

Volatile is one of the important Java language Keywords. Utilizing volatile is yet another way (like synchronized, atomic wrapper) of influencing class to thread safe. Thread safe implies that a strategy or class case can utilize by numerous strings in the meantime with no issue.
Syntax –

class SharedObj
{
  // Changes made to sharedVar in one thread
  // may not immediately reflect in other thread
  static int sharedVar = 6;
}

Assume that two strings are chipping away at SharedObj. On the off chance that two strings keep running on various processors each string may have its own nearby duplicate of sharedVariable. In the thread that one string adjusts its esteem, the change won’t reflect in the first one in the primary memory quickly.

volatileJava

  • Note that compose of ordinary factors with no synchronization activities, won’t unmistakable to any perusing string (this conduct is called sequential consistency). AlbeiFt most present-day equipment gives great store coherence along these lines most presumably the adjustments in a single reserve reflect in other yet it is anything but a decent practice to depend on equipment for to ‘fix’ a flawed application.

Read Java Inner Class – Types of Inner Classes in Java

class SharedObj
{
  // volatile keyword here makes sure that
  // the changes made in one thread are
  // immediately reflect in other thread
  static volatile int sharedVar = 6;
}

So, this was all about  Java Language Keywords. Hope You like our explanation.

6. Conclusion

Hence, in this tutorial, we learned about the Java Language keywords that we use frequently in Java. Moreover, we saw Abstract keyword, abstract class in Java with Syntax, rule and examples. In addition, we looked at the super keyword, its use with variables, methods and constructors. Further, we studied static keywords in java as static blocks, static method, static variable with the example. Lastly, we discussed the Volatile keyword in Java. Furthermore, if you have any query in this important Java Language Keywords section, feel free to ask in the comment section.

For reference 

Leave a Reply

Your email address will not be published. Required fields are marked *