FAQ: Access, Encapsulation, and Scope - The private Keyword and Encapsulation

This community-built FAQ covers the “The private Keyword and Encapsulation” 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 The private Keyword and Encapsulation

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!

private means only I can access it. Who else would be able to access it and why would they be?

Let’s also confirm that we can use private methods. Call addFunds() to add 5 dollars to myAccount ‘s balance . Then print the balance again to confirm the money was added. Continue to do this work in the main() method of CheckingAccount.java .

My code is this:
public class CheckingAccount{
private String name;
private int balance;

public CheckingAccount(String inputName, int inputBalance){
name = inputName;
balance = inputBalance;
}

private void addFunds(int fundsToAdd){
balance += fundsToAdd;
}

private void getInfo(){
System.out.println(“This checking account belongs to " + name +”. It has " + balance + " dollars in it.");
}

public static void main(String args){
CheckingAccount myAccount = new CheckingAccount(“XYZ”, 2024);
System.out.println(myAccount.balance + myAccount.addFunds(5));
}
}

But for some reason it keeps showing me this as the result:
CheckingAccount.java:20: error: ‘void’ type not allowed here
System.out.println(myAccount.balance + myAccount.addFunds(5));
^
1 error

Other attempts include matters such as missing ;, which confused me quite a bit as I made sure my syntax was “accurate”. I am wondering where I went awry? Thank you so much!

Here is another one of my various attempts:

public class CheckingAccount{
private String name;
private int balance;

public CheckingAccount(String inputName, int inputBalance){
name = inputName;
balance = inputBalance;
}

private void addFunds(int fundsToAdd){
balance += fundsToAdd;
}

private void getInfo(){
System.out.println(“This checking account belongs to " + name +”. It has " + balance + " dollars in it.");
}

public static void main(String args){
CheckingAccount myAccount = new CheckingAccount(“XYZ”, 2024);

System.out.println(myAccount.balance + myAccount.addFunds(5));

}
}

It printed out something like this:

Output-only Terminal
Output:
CheckingAccount.java:20: error: ‘;’ expected
addFunds myAccount.addFunds = new addFunds();
^
1 error

And am not sure what it means…

public class CheckingAccount{
private String name;
private int balance;

public CheckingAccount(String inputName, int inputBalance){
name = inputName;
balance = inputBalance;
}

private void addFunds(int fundsToAdd){
balance += fundsToAdd;
}

private void getInfo(){
System.out.println(“This checking account belongs to " + name +”. It has " + balance + " dollars in it.");
}

public static void main(String args){
CheckingAccount myAccount = new CheckingAccount(“Lisa”, 2000);
System.out.println(myAccount.balance);
myAccount.addFunds(5);
System.out.println(myAccount.balance);
}

}

I think there are a couple errors in this lesson. Here’s the pre-written code I find when first reaching this lesson:

//Bank.java
public class Bank{
  public static void main(String[] args){
    CheckingAccount accountOne = new CheckingAccount("Zeus", 100);
    CheckingAccount accountTwo = new CheckingAccount("Hades", 200);
    CheckingAccount myAccount = new CheckingAccount("Mike", 300);
    myAccount.printBalance();  
  }

Here’s the pre-witten code found in the CheckingAccount.java file:

//CheckingAccount.java
public class CheckingAccount{
  private String name;
  private int balance;
  
  public CheckingAccount(String inputName, int inputBalance){
    name = inputName;
    balance = inputBalance;
  }
  
  private void addFunds(int fundsToAdd){
    balance += fundsToAdd;
  }

  public void printBalance(){
    System.out.println("Account balance is " + balance);
  }
  
  private void getInfo(){
    System.out.println("This checking account belongs to " + name +". It has " + balance + " dollars in it.");
  }
}

The instructions say:

  1. We’ve changed the variables and methods in CheckingAccount.java back to private. To begin, go to Bank.java and run your code. Let’s confirm that we get errors when a Bank tries to access private data from a CheckingAccount

Well, when I run Bank.java, making sure I clicked its tab before,
a) A tooltip tells me to

Make sure to run Bank.java, not a different file.

and b) There is no error. The terminal actually says Account balance is 300
In addition, I can’t continue the exercise, I get the red cross

What am I to make of this?

The Bank class tries to access the CheckingAccount’s printBalance() method which works fine, because it’s already public and hence no error.

Further instructions say:

  1. Go to CheckingAccount.java. In the main() method, create a new CheckingAccount named myAccount. You’ll have to use the constructor that takes a String and an int.
    Run CheckingAccount.java to create this object.

The CheckingAccount class lacks a main() method from the outset, so running it throws an error first. I created it and followed the instructions, so it’s fine, but I feel like typically, that should be included in the instructions, following the style of other lessons.

To be able to continue to the next lesson, I replaced my code with the solution code, but it turns out the solution is also off (I figure it’s actually the one form the previous lesson, which has similar problems).
If you follow the instructions exactly, you will find that your code also doesn’t match the solution.
Dear Codecademy, please fix this lesson!

I also have the same problem as well.

hello,
im having the same issue, so how do we continue the exercise?

how did you pass this step /* There is no error. The terminal actually says Account balance is 300
In addition, I can’t continue the exercise, I get the red cross

What am I to make of this?

The Bank class tries to access the CheckingAccount’s printBalance() method which works fine, because it’s already public and hence no error.*/

Can you share the following information?

  • Link to the exercise
  • Which step is not being accepted and what does the feedback message at the bottom say?
  • Can you share all of your code? (To preserve code formatting in forum posts, see: How do I format code in my posts?)
public class Bank{
  public static void main(String[] args){
    CheckingAccount accountOne = new CheckingAccount("Zeus", 100);
    CheckingAccount accountTwo = new CheckingAccount("Hades", 200);
    CheckingAccount myAccount = new CheckingAccount("Mike", 300);
    myAccount.printBalance();
  }
this is printed on the terminal
Account balance is: 300

it is for this exercise
ACCESS, ENCAPSULATION, AND SCOPE

The private Keyword and Encapsulation

interaction # 3
In the main() method of Bank.java , use the printBalance() method to print out the balance of myAccount .

In your last post, your output statement seems to have a colon,

Account balance is: 300

whereas Step 2 doesn’t specify a colon in the output string:

scrn1

thanks.it worked out perfectly. i didn’t know what i was doing wrong.

1 Like