Global variables in java
I want to make a class in java that is accessible to all other classes in my project.
I created this in the default package and now it cannot be seen. What is the best way开发者_运维百科 to do this in java?
Typically the default package is not used, your package would be something like 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.
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();
精彩评论