1
Practice
└─src
  ├─main
  │ └─java
  │   ├─GridPrac.java
  │   └─MyFrame.java
  └─test
    └─java
      └─Test.java

This is my Java project structure. (Sorry, I do not have enough reputation to upload image. :D)

I want to import class from MyFrame.java to Test.java.

When MyFrame class under MyFame.java is public:

public class MyFrame{...}

Eclipse does not generate an error notification.

However, when the MyFrame class under MyFame.java is not public,

class MyFrame{...}

Eclipse generates an error notification.

Can I import public classes from different custom packages under the same Project source folder?

Or, how can I import non-public classes from different folder in the same Project?

Thank you in advance!

:D

Holger
  • 285,553
  • 42
  • 434
  • 765
ghchoi
  • 4,812
  • 4
  • 30
  • 53
  • If both classes are within the same *package*, there is no need for importing anything, the folder structure doesn’t matter. And if you received an error notification you should include the error message in your question. – Holger Nov 07 '14 at 13:57

3 Answers3

4

The problem is that your classes are all in the default package, which is discouraged. Java also doesn't allow you to access classes in that package by normal means. For more information refer to this thread and the linked spec: How to import a class from default package

Besided that, if your class is not public it is not meant to be accessed by classes that are outside the package and don't extend that class. That's part of the language and not a problem of eclipse.

So you if you want to access MyFrame from Test and both are in different packages, MyFrame needs to be public. In your case src/main/java would not be a package unless you don't stick to the conventions, which I assume isn't the case or otherwise you'd probably have different names anyway.

Community
  • 1
  • 1
Thomas
  • 87,414
  • 12
  • 119
  • 157
0

"how can I import non-public classes from different folder in the same Project?". The answer is you can't. In Java, external classes can be either public or default (no access modifier). When a class has default accessibility, it is called package-private. What this mean is that a package-private class can only be visible to other classes inside the same package. For example

package foo;

public class PublicClass {...}

Public class can be accessed by anyone. However,

package bar;

class PackagePrivateClass {...}

can be accessed only by classes inside the bar folder (or package). Therefore, PublicClass cannot import PackagePrivateClass because they are in different packages.

Does that answer your question?

hfontanez
  • 5,774
  • 2
  • 25
  • 37
0

The default setting of a Java class/field/method (when you don't define it as public etc), is 'package-private' which means that only classes that exist in the same package can access them.

If they are in the same package, two classes can always see each other without the need for import. Otherwise, a class must be public to be accessed by another class outside of the package, in which case, yes you must use an import.

hfontanez
  • 5,774
  • 2
  • 25
  • 37
FelixMarcus
  • 380
  • 3
  • 14