Questions tagged [static-import]

Static import is a feature introduced in the Java and C# programming languages that allows members (fields and methods) defined in a class as public static to be used in the code without specifying the class in which the field is defined (introduced in Java 5.0 and C# 6.0).

56 questions
475
votes
8 answers

What does the "static" modifier after "import" mean?

When used like this: import static com.showboy.Myclass; public class Anotherclass{} what's the difference between import static com.showboy.Myclass and import com.showboy.Myclass?
JSON
  • 5,131
  • 4
  • 20
  • 15
159
votes
16 answers

What is a good use case for static import of methods?

Just got a review comment that my static import of the method was not a good idea. The static import was of a method from a DA class, which has mostly static methods. So in middle of the business logic I had a da activity that apparently seemed to…
Miserable Variable
  • 28,432
  • 15
  • 72
  • 133
87
votes
5 answers

Finding import static statements for Mockito constructs

I'm trying to crash through the brick wall between me and Mockito. I've torn my hair out over trying to get correct import static statements for Mockito stuff. You'd think someone would just throw up a table saying that anyInt() comes from…
Russ Bateman
  • 18,333
  • 14
  • 49
  • 65
58
votes
5 answers

static imports in c#

Does C# has feature like Java's static imports? so instead of writing code like FileHelper.ExtractSimpleFileName(file) I could write ExtractSimpleFileName(file) and compiler would know that I mean method from FileHelper.
IAdapter
  • 62,595
  • 73
  • 179
  • 242
32
votes
6 answers

static import only from classes and interfaces

My code compiles fine in Eclipse, but when I try to compile from the commandline (via our ruby-based buildr system), I get this error message: static import only from classes and interfaces Suggesting that static import of public static fields is…
Carl Manaster
  • 39,912
  • 17
  • 102
  • 155
30
votes
3 answers

Why are static imports of static methods with same names legal?

Lets say we have these packages and classes: package p1; public class A1 { public static void a() {} } package p2; public class A1 { public static void a() {} } package p3; import static p1.A1.a; import static p2.A1.a; public class A1…
Janek
  • 1,441
  • 2
  • 19
  • 28
29
votes
8 answers

Are there any advantages of using static import over import?

Consider the following class public final class Constant { public static final String USER_NAME="user1"; //more constant here } This class in the package B. Now I am going to use this in package A. Consider following two ways which can…
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
27
votes
4 answers

Java static imports

Just by experiment I discovered that Java non static methods overrides all same named methods in scope even at static context. Even without allowing parameter overloading. Like import java.util.Arrays; import static…
Stan Kurilin
  • 15,614
  • 21
  • 81
  • 132
25
votes
4 answers

import a static method

How can I import a static method from another c# source file and use it without "dots"? like: foo.cs namespace foo { public static class bar { public static void foobar() { } } } Program.cs using foo.bar.foobar;…
thkang
  • 11,215
  • 14
  • 67
  • 83
17
votes
2 answers

Custom Java query class (DSL): Builder pattern, static imports or something else for complex queries?

I am creating a custom query class, and i am unsure about the most elegant way to code it. The goals are: Easy to use Extensibility Flexible so that complex queries can be formulated Approaches Currently i can think of two alternatives. 1.…
Alp
  • 29,274
  • 27
  • 120
  • 198
17
votes
4 answers

How to stop highlights of static import methods/constants in Android Studio?

I am using static imports in Android Studio as: import static android.opengl.GLES20.*; But android studio highlighting every static member/constant related to static import in code as. I've also tried to look for an option in Settings > Editor >…
Vikas Patidar
  • 42,865
  • 22
  • 93
  • 106
13
votes
5 answers

How can I call a Generic method with a type, when it's statically imported?

I found that you can call a generic method with a special Type, e.g.: suppose we have a generic method: class ListUtils { public static List createList() { return new ArrayList(); } } we can call it like: List
Visus Zhao
  • 1,144
  • 2
  • 12
  • 25
11
votes
1 answer

Replace import of constant/method by static import in IntelliJ Idea

I have a code with some constants in format ClassName.CONSTANT_NAME. I want to quickly replace this by CONSTANT_NAME. But I have not found any efficient way of doing that. Every time I want to static import something I have to remove class name…
Sasha Shpota
  • 9,436
  • 14
  • 75
  • 148
11
votes
6 answers

Can I do a static import of a private subclass?

I have an enum which is private, not to be exposed outside of the class. Is there anyway I can do a static import of that type, so that I don't have to type the enum type each time? Or is there a better way to write this? Example: package…
Kip
  • 107,154
  • 87
  • 232
  • 265
8
votes
4 answers

import static does not work when the class has methods with the same name as the imported ones

I have a Junit4 test case which statically imports the org.junit.Assert.assertEquals method(s). import static org.junit.Assert.assertEquals; In this class I have created a utility method for asserting some complex internal classes which do not…
RonK
  • 9,472
  • 8
  • 51
  • 87
1
2 3 4