Fiveable
Fiveable

Accessor Method

Definition

An accessor method, also known as a getter method, is a public method in Java that allows the retrieval of the value of an instance variable. It provides indirect access to private variables by returning their values.

Analogy

Think of an accessor method like a vending machine. The vending machine has a series of buttons (methods) that allow you to get specific items (values). Just like how you can't directly access the items inside the vending machine without pressing the buttons, you can't directly access private variables without using accessor methods.

Related terms

Mutator Method: A mutator method, also known as a setter method, is another public method in Java that allows modification or update of the value of an instance variable. It provides indirect access to private variables by allowing them to be changed.

Instance Variable: An instance variable is a variable declared in a class and each object or instance created from that class will have its own copy of that variable.

Encapsulation: Encapsulation is one of the four fundamental principles in object-oriented programming. It refers to bundling data and methods together within a class and controlling access to them through encapsulation mechanisms such as private variables and accessor/mutator methods.

collegeable - rocket pep

Are you a college student?

  • Study guides for the entire semester

  • 200k practice questions

  • Glossary of 50k key terms - memorize important vocab



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