💻AP Computer Science A
4 min read•Last Updated on June 18, 2024
Avanish Gupta
Milo Chang
Avanish Gupta
Milo Chang
In this topic, we will take a break from writing our two classes and have a quick discussion on scope and access. Scope and access are two terms that refer to where a variable can be used.
There are two types of scope: local scope and global scope.
Local scope means that a variable can only be used inside a particular method or constructor and not outside. These include our method and constructor parameters and also any variables we declare inside the method or constructor.
On the other hand, global scope means that a variable or method can be used outside that method or constructor and at least throughout that class. These include our instance variables and also the methods that we write for our class. We declare these directly in the class and not inside a method or constructor.
If there is a local variable (usually a parameter) and a global variable (usually an instance variable) in the same method with the same name, the local variable takes precedence. However, it is the convention for our constructor and mutator parameters to have the same names as our instance variables! How can this be possible? Find out in the next topic!
There are different types of global variables which are distinguished by their access modifier. An access modifier basically tells other users and clients where that variable or method can be used outside of that class. There are four types of access: private, package, protected, and public. Those were listed in descending order of restrictiveness.
Private access means that use of the variable or method is restricted to that class. The most notable ones are the instance variables that we have been using. However, methods can also be private. These are usually methods solely created for use by other methods in the class.
The default access is package access, which is the access given if there is no access modifier given. This allows other classes only in that package, or folder, to access these methods and variables. This isn't used too commonly in programming.
Protected access is like package access, but unlike package access, it also allows access from outside the package in one other situation as well, which is when the external class is a subclass of the class which the variable or method in question is located. We will learn about subclasses in Unit 9. Like package access, this isn't used commonly.
Finally, we have public access. Public access is basically what it sounds like — it gives access to the public, which is any class in Java. With public access, other users can change and view the value of a variable and call the method from anywhere. Many of our methods and constructors are public.
Let's take a look at snippets of the Student class we've been working on to identify some of the concepts we've just talked about.
/** Represents a high school student
*/
public class Student {
private int gradeLevel; // a grade between 9-12
private String name; // the students name in the form "FirstName LastName"
private int age; // the student's age, must be positive
private Assignment assignment; // the current assignment the student is working on
private int assignmentsComplete; // numbers of assignments completed
private int correctAssignments; // number of correct assignments
**private static final double A_BOUNDARY = 0.9;
private static final double B_BOUNDARY = 0.8;
private static final double C_BOUNDARY = 0.7;
private static final double D_BOUNDARY = 0.6;
private static String school = "The Fiveable School";**
// More code follows
This first section defines all of the global instance variables and static variables in our Student class. As you can see, we use the private access modifier for all of the variables. This means:
/** Makes a new student with grade gradeLev, name fullName, and age ageNum
*/
public Student(int gradeLev, String fullName, int ageNum) {
gradeLevel = gradeLev;
name = fullName;
age = ageNum;
assignment = null; // There is no active assignment at the moment
assignmentsComplete = 0; // no assignments complete yet
correctAssignments = 0;
}
// More code follows
This next section shows us the Student constructor. The parameters passed into the constructor have a local scope and cannot be accessed outside of this section. The constructor is a public method and can be called from anywhere.
A constructor is a special method within a class that is used to initialize objects of that class. It is called automatically when an object is created and helps set initial values for its attributes.
Term 1 of 10
A constructor is a special method within a class that is used to initialize objects of that class. It is called automatically when an object is created and helps set initial values for its attributes.
Term 1 of 10
A constructor is a special method within a class that is used to initialize objects of that class. It is called automatically when an object is created and helps set initial values for its attributes.
Term 1 of 10
Scope refers to the visibility and accessibility of variables, functions, and objects within a program. It determines where in the code these entities can be accessed and used.
Local Scope: Local scope is an area within a specific block or function where variables are defined and accessible. Variables declared inside this local scope cannot be accessed outside of it.
Global Scope: Global scope refers to the area outside of any blocks or functions where variables are defined. Variables declared in the global scope can be accessed from anywhere in the program.
Enclosing Scope: Enclosing scope refers to an outer scope that contains another inner scope. Inner scopes have access to variables defined in their enclosing scopes.
Local scope is an area within a specific block or function where variables are defined and accessible. Variables declared inside this local scope cannot be accessed outside of it.
Scope Chain: The concept of the "scope chain" describes how JavaScript searches for variable references by traversing through nested scopes until it finds the desired variable.
Block Scope: Block scope is similar to local scope but specifically refers to variables declared within curly braces ({}) in languages like JavaScript or C++. These variables are only accessible within that specific block.
Function Scope: Function scope is a type of local scope that applies specifically to variables declared inside functions. These variables exist only within the function and cannot be accessed outside of it.
Global scope refers to the area outside of any blocks or functions where variables are defined. Variables declared in the global scope can be accessed from anywhere in the program.
Local Scope: Local scope is a restricted area within a block or function where variables are defined and accessible. Unlike global scope, local scope is limited to specific portions of code.
Namespace: A namespace is a container that holds a set of identifiers (variables, functions, classes) to prevent naming conflicts between different parts of a program. It helps organize and group related entities together.
Module Scope: Module scope refers to the visibility and accessibility of variables within a module or file. Variables declared at this level can be accessed by other modules if explicitly exported but remain hidden from other parts of the program by default.
A method is a named sequence of instructions that can be called or invoked to perform a specific task or action. Methods are used for code reusability, organization, and abstraction.
Parameter: A parameter is an input variable passed into a method when it is called. It allows data to be passed from one part of the program to another.
Return Type: The return type specifies the type of value that will be returned by a method after it completes its execution.
Void Method: A void method does not return any value after execution; it performs certain actions but does not produce an output.
A constructor is a special method within a class that is used to initialize objects of that class. It is called automatically when an object is created and helps set initial values for its attributes.
Class: A class is a blueprint or template for creating objects in object-oriented programming. Constructors belong to classes and define how objects of that class should be initialized.
Instance variables: Instance variables are attributes or properties associated with each instance (object) of a class. Constructors often assign initial values to these instance variables.
Overloading: Constructor overloading allows multiple constructors within the same class, but with different parameter lists. This provides flexibility in creating objects with varying initialization options.
Parameters are variables declared in a method or function that receive values when the method is called. They allow data to be passed into a method, enabling it to perform actions or calculations based on those values.
Arguments: Arguments are actual values passed into a method when it is called. They correspond to the parameters defined in the method's declaration.
Return type: The return type specifies the type of value that a method will return after its execution. Parameters help determine what inputs are needed for the method, while return types define what output can be expected.
Method signature: A method signature consists of its name and parameter list. It uniquely identifies a specific method within a class and helps differentiate it from other methods with similar names but different parameters.
Variables are named storage locations in computer memory that hold values which can be changed during program execution.
Data Types: Data types define what kind of data can be stored in a variable, such as integers, strings, or booleans.
Assignment Operator (=): The assignment operator is used to assign a value to a variable.
Scope: Scope refers to the visibility and accessibility of variables within different parts of your code.
Public access refers to the visibility and accessibility of a class, method, or variable within a program. When something is declared as public, it can be accessed from anywhere in the program.
Subclass: A subclass is a class that inherits properties and behaviors from another class. It extends the functionality of the superclass.
Static Variables: Static variables are variables that belong to the class itself rather than instances of the class. They are shared among all instances of the class and can be accessed without creating an object of that class.
Private Access: Private access restricts visibility to only within the same class. It cannot be accessed or modified by other classes or objects outside its scope.
Private access refers to the level of accessibility that restricts a class member from being accessed outside of its own class. It is only accessible within the same class.
Package Access: Package access allows class members to be accessed by other classes within the same package, but not from classes in different packages.
Protected Access: Protected access allows class members to be accessed within the same package or by subclasses even if they are in different packages.
Public Access: Public access allows unrestricted accessibility to a class member from any other class, regardless of package or subclass relationship.
A non-static method is a method that belongs to an instance of a class and can only be accessed through an object. It operates on the specific data of that object.
Static Method: A static method belongs to the class itself rather than any specific instance. It can be accessed without creating an object.
Instance Variable: An instance variable is a variable that holds unique data for each instance of a class. Non-static methods often operate on these variables.
Object: An object is an instance of a class, created using the blueprint provided by the class definition. Non-static methods are called on objects to perform actions or access data specific to that object.