💻AP Computer Science A
4 min read•Last Updated on June 18, 2024
Avanish Gupta
Milo Chang
Avanish Gupta
Milo Chang
We are almost done with our classes, with only two more rounds of edits to make, one in this topic and the next in Topic 5.9. First, we will need to learn more about static variables and methods.
Static variables and methods are marked by the keyword static, which means that these are properties of the entire class and not just of one particular object. For example, static variables and methods are for things that are true for every student, such as boundaries for certain letter grades which we will implement in Topic 5.9, and also the school that the student goes to, which we will implement in this Topic.
Another example of static variables and methods is the Math class we learned back in Unit 2. There are no separate "math objects," but every method is just a general math method doing a calculation.
It is important to note:
We will add static variables and methods to our Student class. Here is a summary of what we will add: grade boundaries for letter grades, a method to get the letter grade for a student, the school name of all students, and also a method to change this school name.
/** Represents an assignment that a student will complete
*/
public class Assignment {
private boolean correctAnswer; // represents the answer to an assignment, either T/F
/** Makes a new assignment with one True/False question and sets the correct answer
*/
public Assignment(boolean answer) {
correctAnswer = answer;
}
/** Prints details about the assignment
*/
[@Override](https://www.fiveableKeyTerm:@Override)
public String toString() {
return "This is an assignment with correct answer " + answer;
}
/** Grades an assignment, returns true if correct, false if incorrect
*/
public boolean gradeAssignment(boolean studentAnswer) {
return studentAnswer == correctAnswer;
}
}
/** 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";**
/** 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;
}
/** Returns the student's grade level
*/
public int getGradeLevel() {
return gradeLevel;
}
/** Returns the student's name
*/
public String getName() {
return name;
}
/** Returns the current assignment the student is working on
*/
public Assignment returnCurrentAssignment() {
return assignment;
}
/** Prints details about the student
*/
@Override
public String toString() {
return name + ", a " + gradeLevel + "th grade high school student has an average grade of " + averageGrade + ".";
}
/** Changes the student's name
*/
public void setName(String fullName) {
name = fullName;
}
/** Changes the student's grade level
*/
public void setGradeLevel(int gradeLev) {
gradeLevel = gradeLev;
}
/** Submits an assignment
*/
public void submitAssignment() {
boolean grade = assignment.gradeAssignment();
assignmentsComplete++;
if grade {
correctAssignments++
}
}
/** Calculates the student's grade as a decimal
*/
public double getGradeDecimal() {
return (double) correctAssignments / assignmentsComplete;
}
**/** Changes the school that the students go to
*/
public static void setSchool(String schoolName) {
school = schoolName;
}**
}
To use static variables and methods, we use the class name and the dot operator, since static variables and methods are associated with a class and not objects. To show you concrete examples of a few concepts, let's assume we are in a main method where the following Student objects are created:
Student alice = new Student(11, "Alice", 17);
Student bob = new Student(10, "Bob Smith", 16);
If we wanted to change the school that all objects of the Student class are associated with, we would do the following in the main method we are in:
Student.setSchool("New School");
Note that instead of doing alice.setSchool("New School"); or bob.setSchool("New School");, we used the class name (Student) and the dot operator.
Imagine for a second that we had this line under private static String school = "The Fiveable School";:
public static String welcomeMessage = "Welcome to School";
We could then use the following line in the main method we are in to print out the welcome message:
System.out.println(Student.welcomeMessage);
(We made welcomeMessage a public variable so we wouldn't have to write with a static getter method to retrieve a private message. We'll learn about scope and access in the next topic.)
The @Override annotation is used in Java to indicate that a method in a subclass is intended to override a method with the same name in its superclass. It helps ensure that the method signature and return type are correct.
Term 1 of 15
Student
class affect the implementation of grade boundaries for letter grades?The @Override annotation is used in Java to indicate that a method in a subclass is intended to override a method with the same name in its superclass. It helps ensure that the method signature and return type are correct.
Term 1 of 15
The @Override annotation is used in Java to indicate that a method in a subclass is intended to override a method with the same name in its superclass. It helps ensure that the method signature and return type are correct.
Term 1 of 15
Student
class affect the implementation of grade boundaries for letter grades?Static variables are variables that belong to the class itself, rather than an instance of the class. They are shared by all instances of the class and can be accessed without creating an object of the class.
Instance Variables: These are variables that belong to individual instances (objects) of a class and have different values for each instance.
Class Variables: Similar to static variables, these are also shared by all instances of a class. However, they can be inherited by subclasses.
Local Variables: These variables are declared within methods or blocks and exist only within their scope. They cannot be accessed outside their respective methods or blocks.
Static methods are methods that belong to the class itself, rather than an instance of the class. They can be called directly using the class name without creating an object of the class.
Instance Methods: These methods belong to individual instances (objects) of a class and operate on specific data associated with those instances.
Constructor Methods: These special methods create objects from a class blueprint and initialize their initial state.
Accessor Methods (Getters) / Mutator Methods (Setters): Accessor methods retrieve values from object attributes while mutator methods modify those attribute values respectively.
Instance variables are variables declared within a class but outside any method. They hold unique values for each instance (object) of the class and define the state or characteristics of an object.
Methods: Methods are actions or behaviors associated with objects in a class. They define what an object can do or how it can interact with other objects.
Constructor: A constructor is a special method within a class that is automatically called when an object is created from that class. It initializes the object's state by assigning initial values to its instance variables.
Access Modifiers: Access modifiers determine the accessibility or visibility of classes, methods, and variables in object-oriented programming. They control which parts of a program can access certain elements and help enforce encapsulation and data hiding principles.
The "this" reference is used in Java to refer to the current object on which a method is being called. It can be used to access instance variables, call other methods, or pass the current object as an argument.
Super Keyword: Used to refer to the superclass or parent class.
Constructor: Special methods used for initializing objects.
Method Overloading: Having multiple methods with the same name but different parameters within a class.