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 () 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 () below!
Agree with a comment or answer? Like () to up-vote the contribution!
}
// main method
public static void main(String args) {
String cookie = “Cookies”;
Store cookieShop = new Store(cookie);
cookieShop.advertise();
}
}
The code from the problem prints “Selling Cookies!”
The question I have is: how does the advertise method use the productType variable to print “Cookies” if the variableStringcookie is defined in the scope of the main method?
Hello @dbss11 and welcome to the Codecademy Forums!
Please format your code using the </> button to make it more readable. Based on what I can see, the following is why advertise is able to use cookie.
When you created the cookieShop instance of the Store class, you passed in cookie (which is equal to "Cookies"). productType is assigned the value of cookie, on the line productType = product; since cookie was passed as the product parameter in the Store class.
I hope this helps and please don’t hesitate to ask for clarification if my explanation was a bit confusing.
This is because we are working off the assumption that it is a new car that has never seen any mileage. Therefore, it’s not necessary to take in the miles driven as an argument since we can just assign milesDriven the value of 0.
Hi @victoria-dr: @dbss11 's confusion may stem from the fact that the tutorial has, thus far, used string literals to create objects. This may be the first time a variable, cookie, was used as argument to create an instance of an object.
I have a different issue: The tutorial refers to a “… faulty print statement from the main() method.” I do not see any. What did I miss?
If you completed step 3 correctly, you should have written a print statement in the main method that results in an error. Step 4 asks you to remove that same print statement.
public class Store {
// instance fields
String productType;
//Assuming here that we are creating an instance to say: “This Store sells a productType” kind of deal.
// constructor method
public Store(String product) {
productType = product;
}
//It’s odd to me that we are saying productType = product? Slightly redundant? Can someone explain why?
Here productType is an instance field of the Store class (or property of Store objects).
So productType or this.productType still exists once the constructor method is finished.
However, product was defined only as a parameter of that constructor, so once that constructor (or method) is finished, the product variable no longer exists. (So you have to copy what’s in product to be in productType.
If cookieShop is an instance of the Store class (meaning cookieShop is a Store object), then it will have its own productType ,
which can be accessed as cookieShop.productType
public static void main(String args) {
String cookie = "Cookies";
Store cookieShop = new Store(cookie);
cookieShop.advertise();
}
In the main method, new Store(cookie) creates a Store object with the cookies as the argument for the constructor.
Since cookie contains "Cookies",
the constructor for Store is called using "Cookies" for the product parameter.
In the constructor, the instance field productType is set to be whatever is currently in product for that one Store object.
So the .productType for that object becomes "Cookies" in the constructor.
Once the constructor is finished executing, cookieShop.productType is "Cookies".
The advertise method for Store objects takes what’s in the productType instance field for that object, puts that into a string, and prints it to the screen/console. CookieShop.productType is "Cookies" at the time that .advertise() is executed for that object,
so in that execution of the advertise method, String message = "Selling " + productType + "!";
is String message = "Selling " + "Cookies" + "!";
so message is "Selling Cookies!" now.
System.out.println(message);
prints that to the screen/console, so you’ll have Selling Cookies! on the screen
when & after cookieShop.advertise(); is executed.
}
So, the purpose was to “replace the productType variable with the product variable inside the advertise() method.” an error will occur due to the product variable not being in the advertise() method’s scope… My question is why is product that is declared in the constructor not accessible in advertise(), but productType is? and does that mean that if i were to later refer back to product that i would be unable to?
productType is accessible in any method (that’s not static) becuase it is declared in the class, but not in a method (making productType an instance field). productType (instance field) could also be accessed as this.productType in the methods.
For the constructor, product is a parameter, so product can only be accessed inside that constructor.
(Yes, you would be unable to refer back to product outside of this constructor.)
Similarly, message is declared in the advertise method, so the variable message would only be available in that method, and nowhere else.