Home | 5.1 | 5.2 | 5.4 | 5.41 | 5.5 | 5.6 | 5.7 | 5.8 | 5.9 | HW |
Accessor Methods
Team Teach
- What is an Accessory Method?
- Purpose of Accessor Methods
- Lets Make Our Own Accessor Methods
- Popcorn Hack
- Things to keep in mind when creating your Accessor Methods
What is an Accessory Method?
An accessor method, also known as getter methods allows other objects to obtain the value of instance variables or static variables.
Purpose of Accessor Methods
- Allows safe access to instance variables
- Accessor Methods keep data access private and secure
- If you need to access instance variables form a different class, accessor methods are necessary
Lets Make Our Own Accessor Methods
public class Minion
{
// Start by defining instance variables that you'll want to access later via the accessor methods
private double height;
private String name;
private String hair;
private int eyes;
// Default Constructor
//String n, int c
public Minion()
{
height = 3.7;
name = "Bob";
hair = "None";
eyes = 2;
}
// Overloaded Constructor
public Minion(double h, String n, String hr, int e)
{
height = h;
name = n;
hair = hr;
eyes = e;
}
// Accessor Methods!
public double getHeight()
{
return height;
}
public String getName()
{
return name;
}
public String getHair()
{
return hair;
}
public int getEyes()
{
return eyes;
}
}
// Create minion object Kevin
Minion kevin = new Minion(4.10,"Kevin","Sprout-Cut",2);
// Print Kevin's properties
System.out.println(kevin.getName());
System.out.println(kevin.getHeight());
System.out.println(kevin.getHair());
System.out.println(kevin.getEyes());
Kevin
4.1
Sprout-Cut
2
Popcorn Hack
Gru is preparing for his big mission to steal the moon, and he needs to assign his minions various tasks based on their skills. Create a new getter method called skillLevel
and print out the Minion’s skillLevel
Minion class with the attributes name, task, and skillLevel.
Implement some getter accessor methods, and then create a Minion
object to retrieve its values.
public class Minion {
private String name;
private String task;
private int skillLevel; // Added skillLevel instance variable
// Constructor to initialize name, task, and skillLevel
public Minion(String n, String t, int s) {
name = n;
task = t;
skillLevel = s;
}
// Getter Methods
public String getName() {
return name;
}
public String getTask() {
return task;
}
// Getter for skillLevel
public int getSkillLevel() {
return skillLevel;
}
public static void main(String[] args) {
// Create Minion object with name, task, and skillLevel
Minion Stuart = new Minion("Stuart", "Developing propulsion system", 85);
// Retrieve and print values
System.out.println(Stuart.getName());
System.out.println(Stuart.getTask());
System.out.println("Skill Level: " + Stuart.getSkillLevel()); // Print skillLevel
}
}
Minion.main(null);
Stuart
Developing propulsion system
Skill Level: 85
Things to keep in mind when creating your Accessor Methods
- The accessor method must be public so that you can retrieve your instance variables outside of the class
- The return Type must match the type of the instance variable accessed
- The naming convention is often getNameOfVariable
- There should be no parameters in your getter methods