As a part of End to End REST Assured Tutorial , in this post We will learn about static import concept in Java which we use in REST Assured mostly.

Static members of class :

When we use “static” modifier with the declaration a member of a class is called as a static member of the 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 object. Static members can be used without instantiating the class i.e. creating an object of class. We can access any static member of class using its class name. We can call static members with an instance of class as well which is not recommended. We do not require to create an object of class to access static members because static members of class is being loaded into memory when class is loaded into memory. Non static members of class is loaded into memory for each instantiation of class.

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

Let’s see some basic examples to understand static members.


Static Import

We know that we can access any static member of a class using its class name. Using class name every time with member name while using in another class of same or different packagees may be hectic. So Java introduced a concept of Static Import in 1.5. You just need to import class as static and you can access all its static members without using class name always. It increases the code readability ( As per official javadoc but I think sometimes it may confuse you a lot. If there are multiple static imports, you can not easily understand members are coming from which class. )

Let’s see a basic example of static import:-

Without static import:-

I want to use static members of class “StaticMembers” in another class. If another class is in same package, you dont require to normal import as well. But if another class is in different package, you need to import that class first.

You can see in both examples above, you need to use class name always. This can be overcome using static import.

With static import:-

You just need to add “static” keyword after “import” keyword and mention either specific static member of class or * to import all. This is difference between normal import and static import.

Disadvantages of static import:-

  1. Excessive use reduce code readability.
  2. Ambiguity occurs when static members with same name are imported from two or more different classes.

You can clone/download example repo here.

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

You can find all Selenium related post here.
You can find all API manual and automation related posts here.
You can find frequently asked Java Programs here.

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

Leave a Reply