AP Computer Science in Java
  • Introduction
  • Introduction to Programming in Java with Karel the Dog
    • Introduction to Programming with Karel
    • More Basic Karel
    • Java Programs and the Run Method
    • Karel Can't Turn Right
    • Methods in Karel
    • Top Down Design and Decomposition in Karel
    • Commenting Your Code
    • SuperKarel
    • For Loops
    • While Loops in Karel
    • If Statements
    • If/Else Statements
    • Control Structures Example
    • How To Indent Your Code
  • Basic Java
    • Printing in Java
    • Variables and Types
    • User Input
    • Arithmetic Expressions
    • Casting
    • Booleans
    • Logical Operators
    • Comparison Operators
    • For Loops
    • While Loops
    • If Statements
    • Loop-and-a-Half
    • Short-Circuit Evaluation
    • De Morgan's Laws
    • Strings
  • Methods
    • Java Methods
    • Methods and Parameters
    • Methods and Return Values
    • Javadoc and More Methods
    • Strings Methods
    • Strings and Characters
    • Exceptions
    • String Processing
  • Classes and Object-Oriented Programming
    • Introduction To Classes and Objects
    • Classes vs. Objects
    • Using a Class as a Client
    • Writing Classes
    • Writing Classes and Instance Methods
    • Getter and Setter Methods
    • Class Methods and Class Variables
    • Method Overloading
    • Local Variables and Scope
    • Key Terms for Classes
    • Objects vs Primitives
    • Inheritance
    • Class Design and Abstract Classes
    • Polymorphism
    • Interfaces
  • Data Structures
    • What Are Data Structures?
    • Introduction to Arrays
    • Using Arrays
    • ArrayList Methods
    • Arrays vs ArrayLists
    • 2D Arrays (Matrices or Grids)
    • Hashmaps
  • Algorithms and Recursion
    • What is an Algorithm?
    • Pseudocode
    • Linear Search
    • Binary Search
    • Selection Sort
    • Insertion Sort
    • Advanced: Recursion
    • Mergesort
Powered by GitBook
On this page
  • Getter Methods
  • Creating Getter Methods:
  • Setter Methods
  • Creating Setter Methods:

Was this helpful?

  1. Classes and Object-Oriented Programming

Getter and Setter Methods

Getter and setter methods allow us to get and set instance variables from the client class.

We use getter and setter methods for validation, to hide internal representation, expansion, and security.

Getter Methods

Getter methods allow us to access specific instance variables of an object. Getter methods are also known as accessor methods.

Creating Getter Methods:

When creating getter methods there is a common convention you should follow. When naming your getter methods you should always use this format: get[Variable Name] for the name. Some examples of this would be: getWidth();, getHeight(), and getColor(). Getter methods usually only return the variable we are trying to get. Here are some examples of how this would look:

private int width = 10;
private int height = 3;
private Color rectCol = Color.blue;

public int getWidth()
{
     return width;
}

public int getHeight()
{
     return height;
}

public Color getColor()
{
    return rectCol;
}

Setter Methods

Setter methods allow us to set the values of an object's instance variables. Setter methods are also known as modifier methods

Creating Setter Methods:

As with getter methods we use a common convention when creating setter methods. When creating setter methods you should always use: set[Variable Name]. Some common examples include: setWidth(int width), setHeight(int height), setColor(Color color). Here are some examples of how to create these setter methods:

private int width = 10;
private int height = 3;
private Color rectCol = Color.blue;

public void setWidth(int newWidth)
{
    width = newWidth;
}

public void setHeight(int newHeight)
{
    height = newHeight;
}

public void setColor(Color color)
{
    rectCol = color;
}
PreviousWriting Classes and Instance MethodsNextClass Methods and Class Variables

Last updated 5 years ago

Was this helpful?