Glossary
Access Modifiers
Keywords in Java that control the visibility and accessibility of classes, variables, methods, and constructors from other parts of the program.
Example:
Using *public* or *private* before a method declaration is an application of access modifiers to control who can call that method.
Global Scope
Variables declared outside of any method or constructor have global scope, making them accessible throughout the entire class.
Example:
A private String *schoolName*; declared directly within a School class has global scope for all methods in that class, allowing them to use schoolName.
Instance variables
Non-static variables declared within a class but outside any method or constructor, belonging to a specific object of that class.
Example:
In a Car class, private int *speed*; is an instance variable because each Car object can have its own unique speed.
Local Scope
Variables declared inside a method or constructor have local scope, meaning they are only accessible within that specific block of code where they are defined.
Example:
In public void printName(String *firstName*) { ... }, the firstName parameter has local scope to the printName method and cannot be accessed outside of it.
Package (Default)
An access level applied when no explicit modifier is used, allowing members to be accessible by other classes within the same package.
Example:
If a class Helper has a method void *doSomething*() { ... } without public or private, it can only be called by other classes in the same package.
Private
An access modifier that restricts access to members (variables, methods) only within the class where they are declared.
Example:
Declaring private String *password*; in a User class ensures that the password can only be directly accessed or modified by methods within the User class itself, promoting data security.
Protected
An access modifier that allows members to be accessed by classes within the same package and by subclasses in any package.
Example:
A protected method *calculateBonus*() in an Employee class could be directly called by a Manager class in the same package or a Contractor subclass in a different package.
Public
An access modifier that makes members (variables, methods, constructors) accessible from any class in any package.
Example:
A public method *deposit*(double amount) in a BankAccount class allows any other part of the program to call it to add money to an account.
Scope
Scope determines where a variable can be used within your code, defining its visibility and lifetime.
Example:
If you declare a variable int score; inside a calculateGrade() method, its scope means it's only visible within that method.