FAQ: Learn Java: Methods - Reassigning Instance Fields

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:

2 Likes

Hi,

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

Br, M.

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

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

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.

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.

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.

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.