简体   繁体   English

Java中的全局变量

[英]Global variables in java

I want to make a class in java that is accessible to all other classes in my project. 我想在Java中创建一个类,我的项目中的所有其他类都可以访问该类。

I created this in the default package and now it cannot be seen. 我在默认程序包中创建了它,现在看不到它了。 What is the best way to do this in java? 用Java做到这一点的最佳方法是什么?

Typically the default package is not used, your package would be something like com.yourdomain.mypackage . 通常,不使用默认软件包,您的软件包将类似于com.yourdomain.mypackage As long as you declare the class as public , it can be seen by all classes as long as they import it. 只要您将该类声明为public ,所有类只要将其导入就可以看到它。

The class would look like 该类看起来像

package com.mycompany.mypackage;

public class MyClass {...}

Then the user of the class would be 那么该类的用户将是

package com.mycompany.anotherpackage;
import com.mycompany.myPackage.MyClass;

private final MyClass myClass = new MyClass();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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