Fiveable
Fiveable
pep
Fiveable
Fiveable

or

Log in

Find what you need to study


Light

5.4 Accessor Methods

5 min readdecember 28, 2022

Athena_Codes

Athena_Codes

Milo Chang

Milo Chang

Athena_Codes

Athena_Codes

Milo Chang

Milo Chang

Introduction to Accessor Methods

For the next couple of topics, we will focus on writing methods. The first type that we will consider are . are methods that allow other clients (objects, classes, and users) to access the data of an object. Since we want other clients to access this, are made . There are two types of :

  • Getter Methods
  • toString()

Getter methods are used when we want to get a specific piece of data, such as the value of a certain . This is done by simply using a in the method and setting the of the method to the type of the variable being returned.

On the other hand, the toString() method returns the information about an object as a string. This is usually done through . As we will see when writing our toString() methods later in the section, we do not need to call the getter methods for specific instance variables. Even though the variables are private, they are in the class, so we can access these variables directly. If we try to print an object in another method, that object's toString() method is automatically called.

Writing Accessor Methods for Our Two Classes

Now, we will write for our two classes. Note that we do not write for all our instance variables because we want some of them to stay private. This will be used with in the and age in the .

Note how we use @Override when we use toString(). We will learn about that in Unit 9!

/** Represents an assignment that a student will complete
*/

Return Expressions

It is important to note the return types of the getter methods we write; these methods are non-void since we want them to return a value. The of a specific getter method is the same type as the whose value we want to get.

  • For example, the getter method that gives us the value of the student's should have a of String. That's why its method header is: String getName().

  • In contrast, the getter method that gives us the gradeLevel for a student should have a of int. That's why its method header is: int getGradeLevel().

When there is a value for the getter method to return, it will return a copy of that value. This is called "return by value."

When the getter method has to return a reference to an object, a copy of that reference is returned, not a copy of the object.

Once the return keyword is triggered in a method, the program immediately returns to the point after whatever called the method.

To see this control flow in action, let's look here. Assume that this is in the main method of some other class:

System.out.println("Starting the example.");

Student bob = new Student(10, "Bob Smith", 16);

System.out.println(bob);

System.out.println("End of example.");

First, the program in the main method will print out "Starting the example."

Next, the program in the main method will create an instance of the by using the . It will initialize gradeLevel to 10, to "Bob Smith," age to 16, and assignment to null. With the creation of this object, the computer will move on to the next line of the main method.

To print out bob, the program in the main method will use the toString() method of the , which will return a String ("Bob Smith, a 10th grade high school student") that will then be printed.

Lastly, the program in the main method will print out "End of example."

Key Terms to Review (15)

@Override

: 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.

Accessor Methods

: Accessor methods, also known as getter methods, are a type of method in object-oriented programming that allows the retrieval of the value of an object's private data member. They provide read-only access to the internal state of an object.

Assignment Class

: An assignment class refers to a class used to store values assigned to variables. It is commonly used in programming to hold and manipulate data.

Assignment constructor

: An assignment constructor is a special type of constructor in object-oriented programming that initializes an object's instance variables with values provided by the user at the time of object creation.

correctAnswer

: A correct answer refers to the accurate response to a question or problem in a given context.

Instance Variable

: An instance variable is a variable that belongs to an object and holds unique data for each instance of the class. It is declared within a class but outside any method.

name

: The name refers to a unique identifier given to an entity, such as a variable or a method, in a programming language. It helps distinguish one entity from another.

Non-void methods

: Non-void methods are functions in programming that return a value after performing a specific task or calculation.

Private Variables

: Private variables are variables that can only be accessed and modified within the same class they are declared in. They provide encapsulation and help maintain data integrity by preventing direct access from other classes.

public

: Public is an access modifier in Java that indicates unrestricted visibility for classes, methods, and variables. Public members can be accessed from any other class or package.

Return Statement

: A return statement is used in functions/methods to specify what value should be sent back as output when the function is called. It terminates the execution of a function and returns control back to where it was called from.

Return Type

: The return type in programming refers to the data type of the value that a method or function will return when it is called. It specifies what kind of data will be returned by the method.

String Concatenation

: String concatenation is the process of combining two or more strings together into one longer string. It is achieved using the "+" operator in most programming languages.

Student class

: A student class is a blueprint or template that defines the properties and behaviors of a student object. It specifies what data (such as name, grade level) and methods (such as calculateGPA()) a student object can have.

Student constructor

: A student constructor is a specific type of constructor used to create objects representing students. It initializes instance variables related to student attributes such as name, age, grade level, etc., when creating new student objects.

5.4 Accessor Methods

5 min readdecember 28, 2022

Athena_Codes

Athena_Codes

Milo Chang

Milo Chang

Athena_Codes

Athena_Codes

Milo Chang

Milo Chang

Introduction to Accessor Methods

For the next couple of topics, we will focus on writing methods. The first type that we will consider are . are methods that allow other clients (objects, classes, and users) to access the data of an object. Since we want other clients to access this, are made . There are two types of :

  • Getter Methods
  • toString()

Getter methods are used when we want to get a specific piece of data, such as the value of a certain . This is done by simply using a in the method and setting the of the method to the type of the variable being returned.

On the other hand, the toString() method returns the information about an object as a string. This is usually done through . As we will see when writing our toString() methods later in the section, we do not need to call the getter methods for specific instance variables. Even though the variables are private, they are in the class, so we can access these variables directly. If we try to print an object in another method, that object's toString() method is automatically called.

Writing Accessor Methods for Our Two Classes

Now, we will write for our two classes. Note that we do not write for all our instance variables because we want some of them to stay private. This will be used with in the and age in the .

Note how we use @Override when we use toString(). We will learn about that in Unit 9!

/** Represents an assignment that a student will complete
*/

Return Expressions

It is important to note the return types of the getter methods we write; these methods are non-void since we want them to return a value. The of a specific getter method is the same type as the whose value we want to get.

  • For example, the getter method that gives us the value of the student's should have a of String. That's why its method header is: String getName().

  • In contrast, the getter method that gives us the gradeLevel for a student should have a of int. That's why its method header is: int getGradeLevel().

When there is a value for the getter method to return, it will return a copy of that value. This is called "return by value."

When the getter method has to return a reference to an object, a copy of that reference is returned, not a copy of the object.

Once the return keyword is triggered in a method, the program immediately returns to the point after whatever called the method.

To see this control flow in action, let's look here. Assume that this is in the main method of some other class:

System.out.println("Starting the example.");

Student bob = new Student(10, "Bob Smith", 16);

System.out.println(bob);

System.out.println("End of example.");

First, the program in the main method will print out "Starting the example."

Next, the program in the main method will create an instance of the by using the . It will initialize gradeLevel to 10, to "Bob Smith," age to 16, and assignment to null. With the creation of this object, the computer will move on to the next line of the main method.

To print out bob, the program in the main method will use the toString() method of the , which will return a String ("Bob Smith, a 10th grade high school student") that will then be printed.

Lastly, the program in the main method will print out "End of example."

Key Terms to Review (15)

@Override

: 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.

Accessor Methods

: Accessor methods, also known as getter methods, are a type of method in object-oriented programming that allows the retrieval of the value of an object's private data member. They provide read-only access to the internal state of an object.

Assignment Class

: An assignment class refers to a class used to store values assigned to variables. It is commonly used in programming to hold and manipulate data.

Assignment constructor

: An assignment constructor is a special type of constructor in object-oriented programming that initializes an object's instance variables with values provided by the user at the time of object creation.

correctAnswer

: A correct answer refers to the accurate response to a question or problem in a given context.

Instance Variable

: An instance variable is a variable that belongs to an object and holds unique data for each instance of the class. It is declared within a class but outside any method.

name

: The name refers to a unique identifier given to an entity, such as a variable or a method, in a programming language. It helps distinguish one entity from another.

Non-void methods

: Non-void methods are functions in programming that return a value after performing a specific task or calculation.

Private Variables

: Private variables are variables that can only be accessed and modified within the same class they are declared in. They provide encapsulation and help maintain data integrity by preventing direct access from other classes.

public

: Public is an access modifier in Java that indicates unrestricted visibility for classes, methods, and variables. Public members can be accessed from any other class or package.

Return Statement

: A return statement is used in functions/methods to specify what value should be sent back as output when the function is called. It terminates the execution of a function and returns control back to where it was called from.

Return Type

: The return type in programming refers to the data type of the value that a method or function will return when it is called. It specifies what kind of data will be returned by the method.

String Concatenation

: String concatenation is the process of combining two or more strings together into one longer string. It is achieved using the "+" operator in most programming languages.

Student class

: A student class is a blueprint or template that defines the properties and behaviors of a student object. It specifies what data (such as name, grade level) and methods (such as calculateGPA()) a student object can have.

Student constructor

: A student constructor is a specific type of constructor used to create objects representing students. It initializes instance variables related to student attributes such as name, age, grade level, etc., when creating new student objects.


© 2024 Fiveable Inc. All rights reserved.

AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.


© 2024 Fiveable Inc. All rights reserved.

AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.