+ 2

Access modifiers public vs. default

Just to be sure: When the parameters or methods of the class are not assigned to a specific access, are they then public? Or it is a default access modifier only availible for the classes in the same package? I don't quite understand where the use of an default access modifier is necessary. Example: class Student { String name; Student (String n) { name = n; } } Cheers.

14th Feb 2018, 4:32 PM
Mirus
Mirus - avatar
1 Antwort
0
Yes you are right, when you don't specify any specific access type for the members of a class, they belong to default access. They are not public. You can use the default access specifier for members of classes which are internal to a particular package and not exposed anywhere outside that package. For example you are writing a private Utility class which has some useful methods required only for a particular package. You can leave the access specifier as empty. Making them public will inadvertently expose them to the outside.
17th Feb 2018, 7:15 PM
Ravi Chandra Enaganti
Ravi Chandra Enaganti - avatar