0

This is my directory structure:

src
|-common
| |-AppManager.java
| |-DesignConstants.java
|-controller
|-view
|-Application.java

I have tried to import "src.Application" class in "src.common.AppManager". But, It's not working. How can I do that?

package common;

import view.MainPanelView;
import Application; // Error :: Class 'Application' is in the default package

public class AppManager {
    private static class Holder {
        public static final AppManager INSTANCE = new AppManager();
        public static final MainPanelView MAIN_PANEL_VIEW = new MainPanelView();
    } // Holder Inner Class

    public static AppManager getInstance() {
        return Holder.INSTANCE;
    } // getInstance()

    public MainPanelView getMainPanelView() {
        return Holder.MAIN_PANEL_VIEW;
    } // getMainPanelView()
} // AppManager Class
dan1st
  • 12,568
  • 8
  • 34
  • 67
JakeKR
  • 1
  • 1

2 Answers2

0

src is not a package but a source folder.`

This means, that AppManager is in the package common and not src.common(see the package declaration: package common on the top of the class).

The Application class however, is in the default package (not a specific package).

Therefore, it has no package declaration at it's top and it can be only used by other classes within the default package.

It is recommended to create a package containing all classes of your project.

For example, you can use this directory structure

src
|-yourrootpackagename
| |-common
| | |-AppManager.java
| | |-DesignConstants.java
| |-controller
| |-view
| |-Application.java

With this structure, you can import yourrootpackagename.Application from yourrootpackagename.common.AppManager.

Don't forget to change the package declarations of your existing classes. For example, the AppManager class should start with:

package yourrootpackagename.common;

By the way, it is a good practice to create a root package containing to reverse-domain-name of your organisation and your project name.

For example, for stack overflow, the package would be something like com.stackoverflow.yourproject. You may want to do that with your reverse domain name.

dan1st
  • 12,568
  • 8
  • 34
  • 67
0

Unfortunately you can't import classes that aren't in a package. See also: https://stackoverflow.com/a/2193298/4425643

You'll have to move Application.java into its own package (folder).

For example, create a folder called main inside the src folder and refactor Application.java by moving it into the package main.