5️⃣

11.5 Access Modifiers

 

Access Modifiers

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.
  1. public - Methods declared as public are accessible from anywhere.
  1. protected - When something is declared using protected, it cannot be accessed from another package (folder/directory).
  1. private - Anything declared as private can only be accessed from within the same class.
  1. 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.
 
notion image
For now, you don’t have to worry that much about access modifiers. You will use them more in Ch. 12.

Static

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).
⚖️
Copyright © 2021 Code 4 Tomorrow. All rights reserved. The code in this course is licensed under the MIT License. If you would like to use content from any of our courses, you must obtain our explicit written permission and provide credit. Please contact classes@code4tomorrow.org for inquiries.