简体   繁体   中英

Static import and class file generation

If we use static import for a class, will the compiler generate a class file for statically imported class when compiling the actual class?

Ex:

import static com.x.y.util.B.getIds();

public class A {
 ...
}

When compiler compiles class A , will it generate class files for B as well?

No, import statement does not cause compiler to generate anything. Think about it: how can compiler generate class if it does not have a source code? Compiler by definition translate source code into executable code (or byte code in case of java).

BTW the syntax of static import in your example is not correct. You should not use () in static import:

import static com.xyutil.B.getIds;

When you use some type in class using key word import or just by full name to it. You must assure that during compilation time, compiler has that both classes in build path.

The static import allow you to access static members of imported class. Nothing more.

class Bar {

 public static int getID() {
    return -1;
 }

}

And for static import

import static Bar.getID;

class Foo {

   private void foo() {
    int id = getID(); //instead of Bar.getID();
   }

}

Read more on Oracle docs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM