Is public keyword is optional in java | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

Is public keyword is optional in java

if we declare any class or method with with public has same meaning without using public key word eg:- public class Name { } or class Name { } is both have same meaning

29th May 2017, 2:33 PM
Rahul Roy
Rahul Roy - avatar
4 Answers
+ 2
No,the default visibility is package-private.That stricter than public. It reachable for the classes from the same package. Here is the documentation: https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html
29th May 2017, 2:45 PM
Szabó Gábor
Szabó Gábor - avatar
+ 1
No!!!
6th Nov 2017, 11:31 AM
Guntas Saran
Guntas Saran - avatar
0
Different. A class declared without public can be called only within the same package. Let's create two classes in two diffierent packages as following. package1.ClassA (with public) package2.ClassB (without public) Try to call ClassA from classB, then classB from classA. You may find the defference.
29th May 2017, 2:46 PM
Twelfty
Twelfty - avatar
0
no, a class variable without public keyword has only scope within a class.
31st Jul 2017, 7:37 PM
Taha Mahmood
Taha Mahmood - avatar