Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

REST Assured Tutorial 3 – Static Import in Java

Posted on 02/19/2025 By admin

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.

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.

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();
                
        }
}

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.

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.

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.*;

// 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();
 }
}

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.

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

Uncategorized

Post navigation

Previous Post: Protractor Tutorial 4 – Introduction To Protractor Test Tool & How It Works
Next Post: Difference Between Page Object Model And PageFactory In Selenium WebDriver

Related Posts

read excel data using apache poi Uncategorized
How To Remove Duplicate Values From List Using Java Stream API? Uncategorized
ConsoleStarted – Make Selenium Easy Uncategorized
DownloadLocationChrome – Make Selenium Easy Uncategorized
promptPopup – Make Selenium Easy Uncategorized
Why Should We Upcast Browser Driver Class Object To WebDriver? Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark