FAQ: Learn Java: Methods - Reassigning Instance Fields

Community%20FAQs%20on%20Codecademy%20Exercises

This community-built FAQ covers the “Reassigning Instance Fields” exercise from the lesson “Learn Java: Methods”.

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

Learn Java

FAQs on the exercise Reassigning Instance Fields

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!

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

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

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!

Ok, I’m getting “Non static variable price cannot be non-static variable price cannot be referenced from a static context
System.out.println(price);”

Error. I’ve obviously goofed something, but I can’t see what the goof is! HELP! (Be gentle, I’m sure it’s going to be something stupid, because I’m on caffeine and nervous energy with this exercise)

public class Store {
// instance fields
String productType;
double price;

// constructor method
public Store(String product, double initialPrice) {
productType = product;
price = initialPrice;
}

// increase price method
public void increasePrice(double priceToAdd){
double newPrice = price + priceToAdd;
double price = newPrice;
}

// main method
public static void main(String args) {
Store lemonadeStand = new Store(“Lemonade”, 3.75);
System.out.println(lemonadeStand.price);
lemonadeStand.increasePrice(1.5);
System.out.println(price);
}
}

Hi!
This is copy-paste from the lesson.

public static void main(String args){
SavingsAccount myAccount = new SavingsAccount(2000);
System.out.println(myAccount.balance);
SavingsAccount.deposit(100);
System.out.println(myAccount.balance);
}

Could you please check if there this line is correct…
SavingsAccount.deposit(100);
Shouldn’t it be: myAccount.deposit(100)?

tnx :slight_smile:

3 Likes

Hi,

try with
System.out.println(lemonadeStand.price);
also in the last line of the main method

Br, M.

2 Likes

Came across the same thing, you can’t call SavingsAccount.deposit(100) in the same way that we can’t use Store.increasePrice(1.5), so I think that needs to be looked into

1 Like

Hi adriennewrn,

The issue is when you created an object of the class Store i.e. “Store lemonadeStand = new Store(“Lemonade”, 3.75)” the name of the thing we need to call the “price” method on is “lemonadeStand”

Such that, what martinam080 said should fix the error as he is now (with dot notation) calling the “price” method on the variable lemonadeStand. It’s also worth noting that when you did create this object aka instance variable you declared with it, it’s type which in this case was Store. Thinking about it this way helps me so I thought it might help you too. In all variable declarations we always include the type whether that is “int,” “double,” “string,” or in this case “Store”

Hope that helps

1 Like

Hey guys,

I use VSCode to do additional Java coding and sure enough when I tried to compile this code it produces that same error. So yup you guys are right. Needs to be changed to:

myAccount.deposit(100)

Most learners may not catch that, especially since you can’t run that code, unless the person is proficient enough to change the class name to match the name of the default folder that’s in the browser.

1 Like

Had the same issue, this was very confusing to me. Because “SavingsAccount.deposit(100);” or would this mean you would deposit “100” to every instance of SavingsAccount?

But then ddcbill’s code should also work, which it doesn’t.

1 Like

Just thought it worth mentioning, on the last step I had the code:
// main method
public static void main(String args) {
Store lemonadeStand = new Store(“Lemonade”, 3.75);
lemonadeStand.increasePrice(1.5);
lemonadeStand.price;

Which correctly supplied an error, yet still considered the task as complete without needing to provide the correction of the line lemonadeStand.price to be System.out.println(lemonadeStand.price), since it technically did not satisfy the request to print out the line until the correction is done.

1 Like

Instead of the clunky:

  public void increasePrice(double priceToAdd){
    double newPrice = price + priceToAdd;
    price = newPrice;
  }

Why not use:

  public void increasePrice(double priceToAdd){
     price += priceToAdd;
  }

It looks far more readable and doesn’t declare new unnecesary variables.

4 Likes

how is price being accessed in the increasePrice() method without using this keyword?

price is an instance field of the current class. If you type no information about where to find the thing you named, like “price”, Java will look at things in the current context by default.

Notably, it will check if the current class doesn’t have fields of this name. Since the current class does have a field named “price”, then Java takes that as being what you meant with the name “price”.

the keyword this is useful in cases where Java would not look at the right place.

Let’s see for example a method like this one:

  public void increasePrice(double price){
     price += price;
  }

In that case, there are two things named “price”. The current class field, but also the current method’s parameter. Java will first look at the current method’s context, and therefore the method’s parameter named “price” will shadow the class’ field “price”. Which means that despite you wanted to update the field, you’re not. You’re just changing the value contained in the parameter variable.

You can solve that situation by telling where to take the thing named “price”, using the this keyword. this means “the current instance of the current class” and this.price means “the field price of the current instance of the current class.”

Therefore, the correct code would be

  public void increasePrice(double price){
     this.price += price;
  }
1 Like

how can price be equal to both intialPrice and newPrice? It doesnt make sense to me that price can be set equal to two different variables.

I have noticed that the grader of these lessons sometimes doesn’t care if I put my semicolons in. It probably should. I am used to writing in kotlin where there are no semicolons.

Hi,

This code (copy pasted from the exercise):

public class Store {
// instance fields
String productType;
double price;

// constructor method
public Store(String product, double initialPrice) {
productType = product;
price = initialPrice;
}

// increase price method
public void increasePrice(double priceToAdd){
double newPrice = price + priceToAdd;
price= newPrice;
}

// main method
public static void main(String args) {
Store lemonadeStand = new
lemonadeStand.increasePrice(1.5);
Store(“Lemonade”, 3.75);

}

gets me this error:
Store.java:21: error: package lemonadeStand does not exist
lemonadeStand.increasePrice(1.5);
^
Store.java:22: error: cannot find symbol
Store(“Lemonade”, 3.75);
^
symbol: method Store(String,double)
location: class Store
2 errors

Could someone please explain where I’m going wrong? Thank you!

This exercise makes no sense to me. I don’t know how it works or why it’s written the way it is.

I’m reading it very slowly, but one parameter (or instance? - there is so much jargon to keep straight) at a time, but I just don’t get it. Maybe its because there are too many similarly named variables (price, newPrice, initialPrice, priceToAdd).

I’m not sure why priceToAdd is created because it’s not called again.

I’m not sure why just typing (1.5) increases the price.

Can anyone explain this to me another way?

Thanks

Should be:
Store lemonadeStand = new Store(“lemonade”, 3.75);

Hi,
Why are we creating and using the newPrice variable inside of the incrisePrice() method if we can get the same result using price = price + priceToAdd;? Am i missing something?

1 Like

Is the first line in the example code missing a class declaration? Ie. shouldn’t it be
public class SavingsAccount{
double balance;
public SavingsAccount(double startingBalance){
balance = startingBalance;
}