I have noticed that if don't write public before a class its works same as like a public class. I can't understand why so? It should show a error when I don't declare a class as public, private or protected. But it works fine. What is the reason?
- 4,634
- 9
- 36
- 47
-
1[This will provide you an adequate answer.](http://stackoverflow.com/a/16728381/1079354) – Makoto May 27 '13 at 19:17
-
You can refer this link, providing the same knowledge: http://stackoverflow.com/questions/614818/what-is-the-difference-between-public-private-protected-and-nothing/614844#614844 – Deepak Jul 26 '16 at 13:10
9 Answers
I have noticed that if don't write public before a class its works same as like a public class.
No it doesn't. Unless it's public, the class won't be visible to other code which isn't in the same package. The default accessibility (which can't be specified explicitly) is that a class (or other member) is only visible to other code within the same package.
You should read the Java Language Specification section 6.6 and the Java Tutorial (Controlling Access to Members of a Class) for more details.
- 1,335,956
- 823
- 8,931
- 9,049
public, protected and private are access modifiers. Public means that the subject may be accessed by any class, protected by subclass, private by the class itself, no modifier means "package protected", so the subject may be accessed by classes from the same package.
Subject is class, method, member variable.
- 111,884
- 15
- 126
- 200
-
2If the subject is a 'top' level class, it cannnot be declared as either `private` or `protected`. – lupchiazoem Oct 28 '18 at 01:46
-
So that would mean that a private class can only ever operate completely independently, right? – temporary_user_name Jan 19 '19 at 01:10
Classes are package private by default (as outlined here) so it's not behaving the same way. You just think it is because you haven't tried to consume your class from a different package.
- 28,584
- 23
- 104
- 139
It works the same only because you are working with just probably one file and in the same package.
If you have more than one package then you have the problem. The class that doesn't have "public" before the name of class cannot be created in another package. You cannot use its constructor. You just cannot access it outside of the package that the class was created in.
- 1,259
- 1
- 16
- 30
For declarations of classes are avaible only two keywords:
- public .Example: public class Student{//...}
- private package(as default) .Example: class Note{//...} .It is visible only in his package.
You can use private and protected only if you declare an member inside of a class. Example:
public class Student{
protected Note note;
}
- 318
- 2
- 4
- 20
If you don't give an access modifier it's by default package private access. The class is not accessible outside of the package. Ideally the JLS should've included a keyword for package access to avoid confusion and unintended consequences.
Something like,
default class Student{}
- 851
- 1
- 8
- 13
No a normal class and a public class don't work the same .A class without a access modifier such as public is automatically set to default access.(No, you can't give default as access explicitly). If you create a default access class inside a package then that class cannot be accessed outside that package but public class can be accessed even outside that package
- 1
- 1
A class not declared with any access modifier is automatically declared as DEFAULT at runtime
With these the class is accessible that same package
But not any other package
- 11
-
1While the answer is not incorrect, it doesn't add any value to the other answers published here. – AlBlue Jan 08 '21 at 11:21