3

For example I have a file Example.java in which there is the following code:

class Example { ... }

And, in another file ExamplePublic.java I have:

public class ExamplePublic { ... }

So, there is no package definition. In this case the classes are in the "unnamed package".

What I want to understand is if in this case the behavior is the same as if the package is specified: the Example class has the "package-private" (default) visibility (in the unnamed package), whereas the ExamplePublic class is accessible from all other packages (because it is public).

Or does anything change for the default package?

Thank you.

Aniket Sahrawat
  • 11,015
  • 3
  • 31
  • 59
Furabio JZ4
  • 145
  • 7

2 Answers2

0

From the official docs Oracle

  • Access level modifiers determine whether other classes can use a particular field or invoke a particular method. There are two levels of access control: At the top level—public, or package-private (no explicit modifier).
  • At the member level—public, private, protected, or package-private (no explicit modifier).

A class may be declared with the modifier public, in which case that class is visible to all classes everywhere. If a class has no modifier (the default, also known as package-private), it is visible only within its own package (packages are named groups of related classes — you will learn about them in a later lesson.)

Dheeraj Joshi
  • 1,310
  • 13
  • 23
0

Java 6 Official document: http://java.sun.com/docs/books/jls/third_edition/html/packages.html#7.5

It is a compile time error to import a type from the unnamed package.

Do Nhu Vy
  • 33,131
  • 37
  • 143
  • 202
  • If you want use trick, you can do like this: http://www.herongyang.com/JDK/Unnamed-Package-Import-from-Unnamed-to-Named-Packages.html – Do Nhu Vy Feb 16 '18 at 13:48