What is the role of static keyword in importing java.lang.System class?

14,794

Solution 1

A static import allows you to write this:

out.print("The result is ");

rather than this:

System.out.print("The result is ");

See e.g. http://docs.oracle.com/javase/1.5.0/docs/guide/language/static-import.html.

Solution 2

Static import is a feature introduced in the Java programming language that allows members (fields and methods) defined in a class as public static to be used in Java code without specifying the class in which the field is defined. This feature was introduced into the language in version 5.0.

The feature provides a typesafe mechanism to include constants into code without having to reference the class that originally defined the field. It also helps to deprecate the practice of creating a constant interface: an interface that only defines constants then writing a class implementing that interface, which is considered an inappropriate use of interfaces.

When you import with static keyword it means you just inserted it somehow in your class and you can use it's methods the same way you're calling your own classes' methods.

For example:

import static java.lang.Math.*;
import static java.lang.System.out;

and :

out.println("I have a house with an area of " + (PI * pow(2.5,2)) + " sq. cm");

Solution 3

I often use static imports in my unit tests, like so:

import static org.junit.Assert.*;

This allows me to write this code:

assertEquals(2, list.size());

Instead of this code:

Assert.assertEquals(2, list.size());

Solution 4

Questions:

1) it works when I import this way:

a. import static java.lang.System.out;
b. import static java.lang.System.*

But doesn't work when I try do this:

c. import java.lang.System.out;
d. import java.lang.System.*;

2) What's the meaning of the static keyword in this particular case?

3) And why import java.lang.*; doesn't import the whole package with System class in it?

--------------------------------------------------------------------------------

Answers:

1) & 2) static imports e.g.(import static java.lang.System.out) are used to import methods or fields that have been declared as static inside other classes in this particular case from the System class.

a. import static java.lang.System.out; //Works because "out" is a static field
b. import static java.lang.System.*; //Works because you are importing ALL static fields and methods inside the System class

c. import java.lang.System.out; //Does NOT work because you are trying to import a static field in a non-static way. (see a.)
d. import java.lang.System.*; //Actually Works because of the * wildcard which allows you to include all imports.

The main reason why you would want to import methods or fields in a static way is so you can omit the class specification for all calls to those methods or fields. So instead of writing:

System.out.print("Hello");
System.out.print("World");

only write

import static java.lang.System.*   //or   import static java.lang.System.out if you only plan on using the 'out' field.
out.print("Hello");
out.print("World");

3) importing java.lang.* is redundant! Java automatically and implicitly imports this package for you! :) and yes it imports the System class with it, but don't get confused unless you imported it as a static import you will still need to write out the long way:

System.out.print("hi");
Share:
14,794
Green
Author by

Green

Node.js Angular, React, React-native JavaScript AWS Full Stack Web Developer from Ukraine.

Updated on June 14, 2022

Comments

  • Green
    Green almost 2 years

    I don't understand the meaning of the keyword static when I import System class:

    import static java.lang.System.*
    

    I'm reading the book about Java and it's written there:

    Any import declaration that doesn't use the word static must start with the name of a package and must end with either of the following:

    • The name of a class within that package
    • An asterisk (indicating all classes within that package)

    For example, the declaration import java.util.Scanner; is valid because java.util is the name of a package in the Java API, and Scanner is the name of a class in the java.util package.

    Here’s another example. The declaration import javax.swing.*; is valid because javax.swing is the name of a package in the Java API, and the asterisk refers to all classes in the javax.swing package.

    And I have the following code:

    public class Addition {
    
       public static void main(String[] args) {
          double num;
          num = 100.53;
    
          num = num + 1000;
    
          // So when I want to import the whole package java.lang as written in the book, it doesn't work:
          // import java.lang.*;
          // or like this:
          // import static java.lang.*;
          // NetBeans in both cases doesn't see these abbreviated names `out` and throws errors. Why?
          out.print("The result is ");
          out.print(num);
          out.println(" .");
       }
    }
    

    And it works when I import this way:

    import static java.lang.System.out;
    import static java.lang.System.*
    

    But doesn't work when I try do this:

    import java.lang.System.out;
    import java.lang.System.*
    

    What's the meaning of the static keyword in this particular case?

    And why import java.lang.*; doesn't import the whole package with System class in it?