Global variables in java

I want to create a class in java that is accessible to all other classes in my project.

I created this in the default package and now it is not visible. What is the best way to do this in java?

+2


a source to share


1 answer


Usually the default package is not used, your package will be something like com.yourdomain.mypackage

. As long as you declare a class as public

, it can be seen by all classes as long as they import it.

The class will look like

package com.mycompany.mypackage;

public class MyClass {...}

      



Then the user of the class will be

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

private final MyClass myClass = new MyClass();

      

+6


a source







All Articles