REST Assured Tutorial 3 – Static Import in Java

Introduction

As a part of the End to End REST Assured Tutorial, in this post, we will learn the static import concept in Java which is mostly used in RestAssured scripts.

Static members of a class

When we use a “static” modifier with the declaration of a member of a class is called a static member of that class. It is also called a class member.

We can use static with a variable, method, inner class, and blocks. Static members are associated with the class, not with the class object. Static members can be used without instantiating the class i.e. creating an object of a class. The reason behind this is that all static members of a class are loaded into memory when the class is loaded in memory. Non-static members of a class are loaded into memory for each instantiation of the class.

We can access any static member of a class using its class name as a reference. We can call static members with an instance of a class as well which is not recommended.

A static variable of a class is shared by each instance of that class. Any object of the class can modify the value of a static variable which will impact all other objects of a class as well as it is a shared variable. The static member variable can be modified without any object as well.

Let’s learn accessing static members of a class through a simple java program.

Java Program

package JavaConcepts;

public class StaticMembers {
	
	// Static variable
	public static String name = "Selenium";
	
	// Non static variable
	public int version = 3;
	
	// Static method
	public static void printName()
	{
		System.out.println("Name is :"+name);
	}

	// Non static method
	public void printVersion()
	{
		System.out.println("Version is : "+version);
	}
	
	
	public static void main(String[] args) {
		
		// Can call static members with class name
		System.out.println(StaticMembers.name);
		StaticMembers.printName();
		
		// Can not call non static members with class name
		//System.out.println(StaticMembers.version);
		//StaticMembers.printVersion();
		
		// Creating an object of class. We can call members using object name
		StaticMembers sm = new StaticMembers();
		
		// Calling static member with object is not recommended
		System.out.println(sm.name);
		sm.printName();
		
		// Object name should be used to call non static members of class
		System.out.println(sm.version);
		sm.printVersion();
		
	}
}

Output

Selenium
Name is :Selenium
Selenium
Name is :Selenium
3
Version is : 3

In the above example program, we called static members of the class using its class name. When we have multiple calls then every time we need to use its class name as a reference. We may end up with multiple boilerplate codes and reduced code readability. We can solve these problems using the static import concept of Java.

Static Import in Java

Java introduced the static import concept in 1.5. Static import allows public and static members i.e. fields and methods of a class to be used in Java code without specifying the class name.

Syntax

If we want to import all public and static members of a class:-

import static pkg1.classname.*;

e.g. import static java.lang.Math.PI;

If we want to import a specific member of a class:-

import static pkg1.classname.someMethod;

e.g. import static java.lang.Math.*;

Example Program

// Class in same package as of StaticMembers
package JavaConcepts;
import static JavaConcepts.StaticMembers.*;
public class WithStaticImport {
 public static void main(String[] args) {
 // No need to Use class name to call static members
 System.out.println(name);
 printName();
 }
}

When to use static import?

Avoid it as much as possible. Static import is a good option when you have 1-2 classes to call static members from. If you have multiple classes from where you need to call methods then instead of increasing readability it will reduce it and it will be confusing for others and for ourselves as well.

As per Oracle doc:-

So when should you use static import? Very sparingly!

In other words, use it when you require frequent access to static members from one or two classes. If you overuse the static import feature, it can make your program unreadable and unmaintainable, polluting its namespace with all the static members you import.

Readers of your code (including you, a few months after you wrote it) will not know which class a static member comes from. Importing all of the static members from a class can be particularly harmful to readability; if you need only one or two members, import them individually. Used appropriately, the static import can make your program more readable, by removing the boilerplate of repetition of class names.

YouTube Video

I have started a YouTube channel as well and you can find related Rest Assured video link here:-

Static import in Java – English

Static import in Java – Hindi

You can download/clone the above sample project from here.

You can subscribe to my YouTube channel RetargetCommon to learn from video tutorials.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappyLearning

Find all Selenium related posts here, all API manual and automation related posts here, and find frequently asked Java Programs here.

Many other topics you can navigate through the menu.

Next Post

First GET REST Assured Test

5 thoughts on “REST Assured Tutorial 3 – Static Import in Java

  1. I always like to watch video to learn , first time reading your blog, I would say simple awesome . enjoying reading. keep it up . thanks

  2. On my second thought while practical usage… its an awesome way to avoid writing the same class name thousand times.

    But yes people should be aware about this concept. As the concept is unique since generally we dont call a method without class name or object…

    So this way is unique and even easier for people to learn and implement it.

  3. I agree with you , i think its better to avoid it unless we have some class which has very unique variable names which would never be used inside class but the chances are rare

Leave a Reply

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