FAQ: Access, Encapsulation, and Scope - Accessor and Mutator Methods

This community-built FAQ covers the “Accessor and Mutator Methods” exercise from the lesson “Access, Encapsulation, and Scope”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Java

FAQs on the exercise Accessor and Mutator Methods

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hello, the answer Codecademy wanted me to use in checkingAccount.java regarding a mutator method is as follows:


public void setBalance(int newBalance) {

balance = newBalance;

}


but at first I wrote:


public int setBalance(int newBalance) {

balance = newBalance;

return balance;

}


Both of these worked but the way I did it at first was deemed wrong. Does anyone know why this is not an acceptable way of creating a mutator method?

How come you cannot print out a void method when using setBalance()? Is it because you’re not returning anything?

The way you did it first is right. The second way is incorrect because the setter method isn’t supposed to return anything.

So this might just be me getting confused on my own, but I feel like the 1st instruction is poorly worded. It says the code is trying to “change” the balance but all it’s trying to do is access the balance. There’s nowhere in the code at the beginning that makes it seem like it’s trying to change the balance. I understand the point it’s going for after finishing the lesson but when first starting out, it’s pretty confusing especially when the 2nd instruction only wants you to access the balance.

1 Like

I agree. I had a question mark appear above my head as well when i read this. As far as i’m aware, you’re not changing the balance instance field yet in that stage of the exercise. Just calling it to see what value it has.

Could you please explain to me why we should write, for example

bankOfGods.accountOne.setBalance(5000);

instead of just

accountOne.setBalance(5000);

?

2 Likes

is it because we have to use an object (ie, bankOfGods)? There are two accounts, so maybe we have to use the created object and specify which account to set?

How does using getters() make it more private when anyone could simply type in the getter to get the balance? Why not just make it public?

Hi there!

I tried to solve this exercise but I am still getting an Error when I use this code for the Setter Method in " CheckingAccount.java":

public void setBalance(int newBalance) {
    balance = newBalance;
  }

Code in Bank.java:

bankOfGods.accountOne.setBalance(5000);
    System.out.println(bankOfGods.accountOne.getBalance());

Error:

Error: Main method not found in class CheckingAccount, please define the main method as:
   public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application

it seems that java expects a main method in CheckingAccount … but why?

Codecademys instruction:

Your method should be a void method — it doesn’t return anything. In Bank.java, use bankOfGods.accountOne.setBalance(5000).

Okay now its working with en extra (empty) Main method ^^

//Write new methods here:
  
  public int getBalance() {
    return balance;
  }

  public void setBalance(int newBalance) {
    balance = newBalance;
  }

  public static void main(String[] args) {
  }

My understanding is that

public void setBalance(int newBalance) {
balance = newBalance;
}

this code is right because we just want to set up the balance, we do not need to have any return value for this method;