2

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?

Paul Sasik
  • 79,492
  • 20
  • 149
  • 189
Xander
  • 9,069
  • 14
  • 70
  • 129
  • 1
    Very similar: http://stackoverflow.com/questions/2030148/whats-the-syntax-to-import-a-class-in-a-default-package-in-java – Pops May 10 '10 at 16:48
  • 1
    good reference. worth noting that you can't import from the default package. – Jeff Storey May 10 '10 at 16:59

1 Answers1

6

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();
Jeff Storey
  • 56,312
  • 72
  • 233
  • 406