ByteBrawl - What Are They - Non Access Modifiers - Encapsulation

Non Access Modifiers - What Are They

Non-access modifiers provide additional functionality and behavior to classes, methods, and variables. There are many of them, but most have very niche and advanced use cases, such as multi-threading and memory management. We'll focus on 3 very commonly used ones: static, final, and abstract.

Where do I put them?

Non-access modifiers are placed before the class, method, or field declaration, in the same place as access modifiers. In fact, the order of access and non-access modifiers doesn't matter, as long as they are before the class, method, or field declaration:

However, as you might have noticed in the code above, the convention is to put access modifiers first, followed by non-access modifiers. This is what you'll see most often in real-world code.

Use for Constructors

Unlike access modifiers, non-access modifiers cannot be used for constructors, and trying to do so will result in a compilation error. This is because their functionalities don't apply to constructors. For example, a static constructor wouldn't make sense because constructors are inherently tied to instances of a class, while static members belong to the class itself. Similarly, constructors are not inherited, so the abstract or final modifiers don't apply to them either.