πŸ“š

Β >Β 

πŸ’»Β 

Β >Β 

βš™οΈ

5.8 Scope and Access

4 min readβ€’december 29, 2022

Peter Cao

Peter Cao

Milo Chang

Milo Chang


AP Computer Science AΒ πŸ’»

130Β resources
See Units

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.

Scope

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!

Access

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.

Example

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:
  • The values of these global instance variables can be accessed or changed from any non-static method within the Student class. As we mentioned in the previous topic, static methods cannot access or change instance variables.
  • The values of these global static variables can be accessed or changed from any method (static or non-static) within the Student class.
/** 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.
Browse Study Guides By Unit
βž•Unit 1 – Primitive Types
πŸ“±Unit 2 – Using Objects
πŸ–₯Unit 3 – Boolean Expressions & if Statements
πŸ•ΉUnit 4 – Iteration
βš™οΈUnit 5 – Writing Classes
⌚️Unit 6 – Array
πŸ’ΎUnit 7 – ArrayList
πŸ’»Unit 8 – 2D Array
πŸ–²Unit 9 – Inheritance
πŸ–±Unit 10 – Recursion
πŸ™Exam Reviews

Fiveable
Fiveable
Home
Stay Connected

Β© 2023 Fiveable Inc. All rights reserved.


Β© 2023 Fiveable Inc. All rights reserved.