Is java an import keyword for source or binary files?

I know I can use the import statement to include a class or group of classes in my java project

For example, import Java io. utils.* Import (can be used in my java program) io. All classes in the utils package

My question is, do I need to compile the classes in the import package? Or can the package also contain uncompiled java files? If it can be, when can we use class files and java files?

Solution

Import simply means "make the imported class available with a simple name" – if you use a fully qualified name anywhere, you can completely delete the import For example, it is definitely not like #include. In C

When you compile, if you try to reference uncompiled code, it will be compiled at that time, assuming that the compiler can guess where to find the source code The result never refers to uncompiled code, because the compiler needs to know what each type exposes

As a complete example, construct the following file structure:

// src/foo/A.java
package foo;
import bar.*;

public class A {
    public static void main(String[] args) {
        B.sayHello();
    }
}

// src/bar/B.java
package bar;
public class B {
    public static void sayHello() {
        System.out.println("Hello");
    }
}

Then run in the SRC Directory:

javac foo/A.java

This will automatically compile bar / b.java – but will not compile any other unreferenced code (possibly passed)

I strongly recommend not using this "compile on demand" behavior - if you compile a Class A that depends on class B, it will compile B the first time, but then if you change B and recompile a, the compiler wins' recompile B. I will organize your code into the appropriate project and always recompile a complete project at a time, Add the output directory of the project to the classpath of the project that depends on it, instead of allowing one project to be compiled to recompile the bits of another project upon request

(please note that this is not to say that many ides support progressive compilation... This is a quite different problem. It's good to assume that it has been implemented correctly.)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>