28

What's the difference between

class x {
    //code here
}

and

public class x {
    //code here
}

Sometimes I see examples on the Internet and they'll have public class instead of class and they're all simple programs. I use class for my assignments and so does everybody else

Alex
  • 3,111
  • 6
  • 27
  • 43
  • 1
    And then: If I have a `class X` (please capitalize the class name) that is not declared as `public`, can I have public instance variables (EG `public String name;`) in it?? ;) – Marsellus Wallace Oct 04 '11 at 00:12

4 Answers4

34

The first one will result in your class being assigned the default visibility, that is package-private (ie: accessible within the same package).

The second one makes it public, that is, visible to any other class.

Reference: Controlling Access to Members of a Class

Community
  • 1
  • 1
NullUserException
  • 83,810
  • 28
  • 209
  • 234
9

The simplest way to put it:

if everything you have is in the same package (package com.myschool.myapp at the top of every file) then there is no difference, but if anything wants to access anything outside it's package then it must be public.

For instance, "String" is in "java.lang", your package almost certainly isn't in java.lang so if string wasn't public you couldn't access it.

Bill K
  • 62,186
  • 18
  • 105
  • 157
5
  • The former is "package visibility" or "default visibility", where the class is only visible to classes in the same package.
  • The latter is "public visibility", where the class is visible to any other class.

There also exists the following visibility modifiers for members and methods and inner classes:

  • "Protected visibility" is the same as package, except that any class that inherits the class defined with protected scope can also access it.
  • "Private visibility" means that the class is only accessible to
    itself.
Jon Newmuis
  • 25,722
  • 2
  • 45
  • 57
  • 1
    -1 You can't declare classes to be `protected` or `private`. What could possibly be the point of having a class visible only to itself? – NullUserException Oct 03 '11 at 23:49
  • 1
    Both answers are correct. One more comment: try putting "public" on *both* classes - or "public" on a class name that doesn't match your .java source file name - and watch the compile error you'll get ;) – paulsm4 Oct 03 '11 at 23:49
  • Almost. A private class is also accessible by its enclosing class. It's also worth noting that each .java file may have only one public class, and if a .java file contains a public class, the file name must match the class name. – Ryan Stewart Oct 03 '11 at 23:53
  • 1
    @NullUser: Certainly you can have protected and private classes. The link you posted in your own answer clearly indicates so. Please remove your downvote. This is a good answer. – Ryan Stewart Oct 03 '11 at 23:54
  • 1
    @NullUserExceptionఠ_ఠ - You can have protected and private classes if they are inner classes. You just cannot have protected or private top-level classes. – Jon Newmuis Oct 03 '11 at 23:56
  • @Null - Since I have created both `protected class`es and `private class`es I can guarantee you that you **can** declare them. They are useful to restrict the scope of non-anonymous *inner* classes. – Stephen P Oct 03 '11 at 23:59
  • @RyanStewart A **top-level** class, which is what the OP is apparently referring to, cannot be declared `protected` or `private`. I removed the downvote anyways – NullUserException Oct 04 '11 at 00:14
0

The first one will be default class, i.e it cannot be accessed in any other package except itself, but can be accessed by other classes in the same package.

The second one makes it public,i.e, you can access it from any class of any package just like you access String and Array which are if different packages.

Remember, we are talking about accessible from packages not class.

Regarding, And then: If I have a class X (please capitalize the class name) that is not declared as public, can I have public instance variables (EG public String name;) in it?? ;)

Yes of course you can have public instance variables in non-public class.

kbharuka
  • 49
  • 7