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

4

8

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?

Diet answered 25/5, 2012 at 14:55 Comment(2)
Why are you importing java.lang.System?Kovar
For now just to have the possibility to write print statement shortly out.print(num); without System word. But then I thought why not to import the whole java.lang package instead of importing only System. I tried and got errors.Diet
K
14

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.

Kosey answered 25/5, 2012 at 14:56 Comment(0)
V
2

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());
Vesicant answered 25/5, 2012 at 15:0 Comment(0)
G
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");
Gamal answered 25/5, 2012 at 15:17 Comment(0)
G
2

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");
Garlen answered 16/11, 2016 at 22:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.