-3

I saw some code over the web like:

import static org.mockito.Mockito.*;

Can someone please tell me what this static means in this case?

The Dr.
  • 556
  • 1
  • 11
  • 24
  • 2
    [http://javapapers.com/core-java/what-is-a-static-import-in-java/](http://javapapers.com/core-java/what-is-a-static-import-in-java/) – Amit.rk3 Aug 02 '15 at 14:11
  • 2
    See [here](http://stackoverflow.com/questions/162187/what-does-the-static-modifier-after-import-mean) and [here](http://stackoverflow.com/questions/420791/what-is-a-good-use-case-for-static-import-of-methods). – Mordechai Aug 02 '15 at 14:13
  • Please so some research before asking a question – SamTebbs33 Aug 02 '15 at 14:17

2 Answers2

3

Generic case:

In order to access static members, it is necessary to qualify references with the class they came from. For example, one must say:

double r = Math.cos(Math.PI * theta);

The static import construct allows unqualified access to static members without inheriting from the type containing the static members. Instead, the program imports the members, either individually:

import static java.lang.Math.PI;

or en masse:

import static java.lang.Math.*;

Once the static members have been imported, they may be used without qualification:

double r = cos(PI * theta);

There is the source here.

Your case:

You might write the following code:

staticMethod();

instead of:

Mockito.staticMethod();
Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
1

Java 5 have introduced static imports which allow the importing of static members and to use them as if they are declared in the class that imported them. For more ref you can refer the java docs

SamTebbs33
  • 5,507
  • 3
  • 22
  • 44
Shriram
  • 4,343
  • 8
  • 37
  • 64