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?
In object-oriented programming, a public class is any part of the program that accesses or updates its members using the member name to be accessed. This type of class is useful for information or methods that need to be available in any part of the program code. Class, Private, Programming terms.
A public class has "public" visibility, which means that it is accessible to any class in any package, while a class has "default" or "package" visibility, i.e. the class is accessible only to classes inside the same package. Also if you are working only on one package then there is no difference, you can use both.
A public class will have access specifier "public" and its members can be accessed with in and out of the class in which they are specified. A class which doesnt have the public access specifier are called as non-public classes and its member can be accessed only within the class in which they are specified.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With