1

Possible Duplicate:
package visibility
Java - The difference between class “ClassName” and public class “ClassName”

Basically is there any difference in these two modifiers of classes? Is there any difference in not having the public modifier?

public class MyClass {}

class MyClass {}
Community
  • 1
  • 1
mark
  • 2,841
  • 4
  • 25
  • 23
  • 1
    @pst: OK, what about these ones: http://stackoverflow.com/questions/8191022/what-are-variables-without-public-private-or-protected-declared-as, http://stackoverflow.com/questions/215497/in-java-whats-the-difference-between-public-default-protected-and-private, http://stackoverflow.com/questions/6996739/non-public-top-level-class-in-java, etc. They all treat the subject sufficiently. I'm sure I can find more. We could discuss this on meta, how "exact" a duplicate should be – Lukas Eder May 14 '12 at 14:17
  • @LukasEder No, that's about members, not classes. –  May 14 '12 at 14:17

4 Answers4

1

A public class is visible by any other class, while a class (without modifier) will have a package visibility.

Romain Linsolas
  • 79,475
  • 49
  • 202
  • 273
0

Default is package-private http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

MaddHacker
  • 1,118
  • 10
  • 17
0

From the Java-Specification:

If a class or interface type is declared public, then it may be accessed by any code, provided that the compilation unit (§7.3) in which it is declared is observable.

If a top level class or interface type is not declared public, then it may be accessed only from within the package in which it is declared.

FrVaBe
  • 47,963
  • 16
  • 124
  • 157
0

1.Public class is accessible from outside its package whereas, class is not.
2.There can be only one public class in a source file, and the source file's name has to be same.

zahreelay
  • 1,742
  • 12
  • 18