[英]How Do I Import Classes From Another Package and Folder in Java?
I just started studying OOP and Packages in Java.我刚开始研究 OOP 和 Java 中的 Packages。 I have a question regarding package importing in Java.我对在 Java 中导入 package 有疑问。
I have two files, named ImportThis.java
and Here.java
我有两个文件,名为ImportThis.java
和Here.java
The directory for ImportThis.java
on my local machine is F:\VS Codes\master\folderone\folderoneone\ImportThis.java
.我本地机器上ImportThis.java
的目录是F:\VS Codes\master\folderone\folderoneone\ImportThis.java
。 And the contents of ImportThis.java
is: ImportThis.java
的内容是:
package master.folderone.folderoneone;
public class ImportThis {
public static void aStaticMethod() {
System.out.println("Hello World");
}
}
The directory for Here.java
on my local machine is F:\VS Codes\master\foldertwo\foldertwotwo\Here.java
. Here.java
在我的本地机器上的目录是F:\VS Codes\master\foldertwo\foldertwotwo\Here.java
。 And the contents of Here.java
is: Here.java
的内容是:
package master.foldertwo.foldertwotwo;
public class Here {
public static void anotherMethod() {
ImportThis.aStaticMethod();
}
}
By looking at the contents of Here.java
, you might be able to tell that I want to import the class ImportThis
from ImportThis.java
to Here.java
, and it is indeed what I've been trying to do. By looking at the contents of Here.java
, you might be able to tell that I want to import the class ImportThis
from ImportThis.java
to Here.java
, and it is indeed what I've been trying to do. But both ImportThis.java
and Here.java
came from different folders and packages.但是ImportThis.java
和Here.java
都来自不同的文件夹和包。 I've tried using import master.folderone.folderoneone.ImportThis;
我试过使用import master.folderone.folderoneone.ImportThis;
on Here.java
but VS Code says it cannot be resolved.在Here.java
但 VS Code 说它无法解决。 Looking forward to the answer for my question!期待我的问题的答案!
EDIT: Changed package names and lowercased the folder names编辑:更改 package 名称并小写文件夹名称
Try:尝试:
package master.foldertwo.foldertwotwo;
import master.folderone.folderoneone.ImportThis; //<-import statement
public class Here {
public static void anotherMethod() {
ImportThis.aStaticMethod();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.