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

This community-built FAQ covers the “Scope: The this Keyword” 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 Scope: The this Keyword

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!

I had a simple question on this exercise. It has you setting the instance variables equal to the passed parameters using the ‘this’ keyword, which conceptually was simple enough. My question was, since one of the instance variables is a String, I thought you could not simply use the ‘=’ and had to use the String built in ‘equals()’ function. Why is this not the case here? Thanks.

1 Like

Hi, I am confused about using the ‘this’ keyword as a parameter. In the example below, does the ‘this’ keyword that is being passed as a parameter in this.methodTwo within method methodOne mean that methodTwo itself is being passed as a parameter?

Perhaps a real world example of when using the ‘this’ keyword as a parameter within a function is accomplishing something meaningful would be helpful. Could anyone provide such an example?

I am testing the balanceEuro in this way and seems to work. Sharing to help others and get feedback:)

public class SavingsAccount{

  public String owner;
  public int balanceDollar;
  public double balanceEuro;

  public SavingsAccount(String owner, int balanceDollar){
    // Complete the constructor
    this.owner = owner;
    this.balanceDollar = balanceDollar;
this.balanceEuro =  balanceDollar * 0.85;

  }

  public void addMoney(int balanceDollar){
    // Complete this method
    System.out.println("Adding " + balanceDollar + " dollars to the account.");
    this.balanceDollar += balanceDollar;
    System.out.println("The new balance is " + balanceDollar + " dollars.");
  }

  public static void main(String[] args){
    SavingsAccount zeusSavingsAccount = new SavingsAccount("Zeus", 1000);
    System.out.println(zeusSavingsAccount.balanceEuro); //balanceEuro test 

    // Make a call to addMoney() to test your method
zeusSavingsAccount.addMoney(2000);
  }

}

In methodOne, when we call methodTwo with this keyword means that yes, we are passing it to methodOne.
So whenever we call methodOne, methodTwo will be called but not as a parameter thought.

It is like a call back if you remember from other programming languages such as Javacript.
In simple words: “Hey methodOne I am calling you so you should call methodTwo”.

This is how I understand it:)

public class SavingsAccount{

  public String owner;
  public int balanceDollar;
  public double balanceEuro;

  public SavingsAccount(String owner, int balanceDollar){
    // Complete the constructor
    this.owner = owner;
    this.balanceDollar = balanceDollar;
    this.balanceEuro = balanceDollar * 0.85;
  }

  public void addMoney(int balanceDollar){
    // Complete this method
    System.out.println("Adding " + balanceDollar + " dollars to the account.");

    this.balanceDollar += balanceDollar;
    System.out.println("The new balance is " + this.balanceDollar + " dollars.");

    this.balanceEuro += balanceDollar * 0.85;
    System.out.println("The new balance is " + this.balanceEuro + " euros.");
  }

  public static void main(String[] args){
    SavingsAccount zeusSavingsAccount = new SavingsAccount("Zeus", 1000);

    // Make a call to addMoney() to test your method
    zeusSavingsAccount.addMoney(2000);
  }

}

Hi pkusaha, Thank you for sharing your code! Can you help me understand why it is necessary to update balanceEuro in the addMoney method with *.85? Why doesn’t it automatically use the formula in the constructor? this.balanceEuro = balanceDollar * 0.85;

I don’t understand this example

public Dog(String inputName){
  this.name = inputName;
}

If the constructor’s parameter is now named differently from the instance variable (inputName instead of name), why should one use this. in the first place?

Why can’t I print out any instance variable? The error says “non-static variable balanceEuro cannot be referenced from a static context”

public class SavingsAccount{

  public String owner;
  public int balanceDollar;
  public double balanceEuro;

  public SavingsAccount(String owner, int balanceDollar){
    // Complete the constructor
    this.owner = owner;
    this.balanceDollar = balanceDollar;
    this.balanceEuro = this.balanceDollar * 0.85;

  }

  public void addMoney(int balanceDollar){
    // Complete this method
    System.out.println("Adding " + balanceDollar + " dollars to the account.");
    this.balanceDollar = this.balanceDollar + balanceDollar;
    System.out.println("The new balance is " + this.balanceDollar + " dollars.\n");
  }

  public void checkBalance(){
    System.out.println("The balance of the account beloging to " + owner + " is " + balanceDollar + " dollars.\n");
  }

  public void checkBalanceInEuros(){
    System.out.println("The balance of the account beloging to " + owner + " is " + balanceDollar + " dollars. Applying the current exchange rate of 0.85, it is " + balanceEuro + " euros.\n");
  }

  public static void main(String[] args){
    SavingsAccount zeusSavingsAccount = new SavingsAccount("Zeus", 1000);

    // Make a call to addMoney() to test your method
    zeusSavingsAccount.addMoney(2000);
    System.out.println(balanceEuro);
  }

}

I can’t checkBalanceInEuros too (it returns an incorrect number, 850)

public class SavingsAccount{ public String owner; public int balanceDollar; public double balanceEuro; public SavingsAccount(String owner, int balanceDollar){ // Complete the constructor this.owner = owner; this.balanceDollar = balanceDollar; this.balanceEuro = balanceDollar * 0.85; } public void addMoney(int balanceDollar){ // Complete this method System.out.println("Adding " + balanceDollar + " dollars to the account."); this.balanceDollar += balanceDollar; System.out.println("The new balance is " + this.balanceDollar + " dollars."); this.balanceEuro = this.balanceDollar * 0.85; System.out.println("Balance in EUR: " + this.balanceEuro + "€"); } public static void main(String[] args){ SavingsAccount zeusSavingsAccount = new SavingsAccount("Zeus", 1000); // Make a call to addMoney() to test your method zeusSavingsAccount.addMoney(2000); } }

tested, and works. for some reason the code isnt recognised though. but it is correct for showing balanceEuro too.

My contribution, one thing i understand, was “this”, you just should use when will change the value of the variable

public class SavingsAccount{

  public String owner;
  public int balanceDollar;
  public double balanceEuro;

  public SavingsAccount(String owner, int balanceDollar){
    // Complete the constructor
    this.owner = owner;
    this.balanceDollar = balanceDollar;
    this.balanceEuro = balanceDollar * 0.85;
  }

  public void addMoney(int balanceDollar){
    // Complete this method
    this.balanceDollar += balanceDollar;
    System.out.println("Adding " + balanceDollar + " dollars to the account.");
    System.out.println("The new balance is " + this.balanceDollar + " dollars.");
  }

  public static void main(String[] args){
    SavingsAccount zeusSavingsAccount = new SavingsAccount("Zeus", 1000);

    // Make a call to addMoney() to test your method
      zeusSavingsAccount.addMoney(2000);
  }
}

There are 3 errors in this lesson.