2

What does mean by static import, like :

import static com.example.foo.Suggestion;

How to define such packages and what is advantages to use static import?

Andremoniy
  • 34,031
  • 20
  • 135
  • 241
Puneet
  • 611
  • 6
  • 22
  • 1
    [Static import](http://docs.oracle.com/javase/1.5.0/docs/guide/language/static-import.html) – Paresh Mayani Jan 08 '13 at 06:11
  • 2
    you sure you don't know how to google this stuff ? – Sreenath S Jan 08 '13 at 06:12
  • Take effort to visit the [link](http://stackoverflow.com/questions/8972179/java-static-imports) – Aniket Kulkarni Jan 08 '13 at 06:32
  • 1
    why people down-vote for this question, are they really not able to understand what am i asking here? May be i am new for this topic and I was not aware about import feature, I am here to learn, even i reply very basic questions asked by people... – Puneet Jan 10 '13 at 04:27

5 Answers5

8

import static means that you can references a static value without using the class name.

For example, consider these three classes:

package com.example;
public class foo {
    public static int Suggestion = 5;
}
import com.example.foo;
public class b {
    // …
    int var = foo.Suggestion;
}
import static com.example.foo.Suggestion;
public class c {
    // …
    int var = Suggestion;
}
5gon12eder
  • 24,280
  • 5
  • 45
  • 92
Andy McSherry
  • 4,676
  • 3
  • 26
  • 36
3

Advantage of static import what I felt is over Constant Interface Pattern.

Generally we use interfaces for constant and it is implemented by all classes but if you are developing an API, it is something like you are exposing your implementation details. Above wiki link explains very well.

But use of static imports avoid this and provide very good solution to constant interface Anti-Pattern. Instead of constant interface pattern I would create a final class, create public static constants in that class, and do static import wherever Constants needed.

Nandkumar Tekale
  • 16,024
  • 8
  • 58
  • 85
2

Static imports allows the static members of a class to be used without the use of full class name as compared to normal import declarations.

Deepak kumar Jha
  • 524
  • 5
  • 16
0

Static imports in Java allow you to refer to static entities (methods, properties) without having to fully qualify the reference in your code. It is syntactic sugar.

EJK
  • 12,332
  • 3
  • 38
  • 55
0

You can use static import only for Static fields and methods. You have to define the methods and

Hulk
  • 152
  • 3
  • 10