Access modifiers (also known as visibility modifiers) are used to control who has access to our code. You can add access modifiers to a class, variable, or method. There are four access modifiers in Java.
public- Methods declared as public are accessible from anywhere.
protected- When something is declared using protected, it cannot be accessed from another package (folder/directory).
private- Anything declared as private can only be accessed from within the same class.
- Default - Something can be described as “default” when no access modifier is specified. Things that are default can be accessed within the same class and package.
For now, you don’t have to worry that much about access modifiers. You will use them more in Ch. 12.
You may have noticed that there is another keyword usually used in methods called
static. This is a modifier related to classes, which we will learn in Ch. 12. For now, just use the
static keyword for all of your methods (after the access modifier).