Make Selenium Easy

REST Assured Tutorial 3 – Static Import in Java

Last updated on November 21st, 2020 at 03:44 pm

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

Output

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

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

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

4 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

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.

REST Assured Tutorial 3 – Static Import in Java

by Amod Mahajan time to read: 4 min
4
%d bloggers like this: