0

I understand the way the four access modifiers work inside classes, however you can also put them before the keyword class, like:

accessModifier class className{
 accessModifier type var;
}

I previously understood access modifiers as the members' visibility, so, e.g., private would mean the variable will only be visible inside the class; but how does that work behind the class?

Also, in what situations should I put the access modifiers behind the class and why?

Duarte Arribas
  • 620
  • 1
  • 23
  • It is like how they are visible between packages – Cimon Oct 18 '20 at 18:18
  • Visibility modifiers work essentially the same for classes as they do for members of classes: other classes can only "see" classes that are visible according to the same rules. – Andy Turner Oct 18 '20 at 18:28

3 Answers3

1

The access modifier applied to a class takes precedence over the access modifiers applied to its fields, methods, etc. So even if you mark a field as public, if your class is marked as having default (package-private access) access, then classes outside the package where your class is located won't be able to access them. The access modifiers private and protected can't be applied to top level classes (i.e. classes that aren't themselves part of another class, unlike for example inner classes), a class can only be public or have the default access modifier. So basically, if you don't know if the class or any of its members or methods are going to be used by anything outside its package, make it public. If you know it's only gonna be used by stuff from the same package, you can make it package-private.

JustAnotherDeveloper
  • 1,575
  • 2
  • 4
  • 18
1

I understand your query. The access modifier for the classes is for package level. If a class is private its is accessible within that class only. If a class is public it is accessible outside the package and also on a global level. If a class is protected it is accessible outside the package. and default class give access within the same package or subclasses.

All this comes into account when dealing with hierarchies level program in detail. if you are a beginner you can start learning private and public and then move to understand protected.

Example: for a real-life example, if you have an account on any website it should be in a private class because only is visible and accessible to you.

if a seller is selling a product it must be placed in public class on the website so as it accessible and operational to all other classes.

I hope you understand. :)

proxyMac
  • 9
  • 2
1

Access modifiers on a class have exactly the same meaning as on members: public is visible to everyone, protected is visible to subclasses and to all classes in the same package, private is visible only within the same source file, and no modifiers at all implies visibility only to other code in the same package.

Top-level classes (that is, classes which are not themselves members of another class) must either be public or have no access modifier at all (meaning, package-visible).

This is described in the Java Language Specification’s section on class declarations.

VGR
  • 33,718
  • 4
  • 37
  • 50