This community-built FAQ covers the “Classes: Constructor Parameters” exercise from the lesson “Java: Introduction to Classes”.
Paths and Courses
This exercise can be found in the following Codecademy content:
Learn Java
FAQs on the exercise Classes: Constructor Parameters
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!
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!
so confusing. how am i the first person to post a reply on this discussion board?
2 Likes
the FAQ feature is reasonable new, many people still make new topics rather then replying to a FAQ topic.
wiki-bot made this topic, we both posted a reply in this topic. But the forum has many topics, so what do you mean by discussion board? Here is a list of the latest topics:
https://discuss.codecademy.com/latest
see? Quite a bit of activity
Anyway, do you have an actually question?
Hi. Thanks for your reply. Im just going through the lessons and then visiting the discussion board associated with each lesson to get clarification on the lessons. Several of them don’t have any comments yet. I didn’t realize others were posting questions elsewhere. I’ll Look at those other questions. 
Jon H
what is the difference between constructor parameter and main() method’s parameter? are they same?
the main method is called by the Java compiler/JVM. Without main, our program won’t run
while the constructor allows you to set instance fields.
I noticed that in the examples the instance field names are usually different from the instance parameters, while at the same time they are all being mapped. Is there any naming convention? I imagine in theory they could all have the same names (eg. product = product;).
6 Likes
why is it not necessary to write - Store variableName = new Store() but just new store() ? how does that work?
I am wondering why keyword .this is ommited in the explanation for that lesson when assigning parameter value to the field. Why is that? And what difference does it make if .this would be added?
public class Car {
String color;
// constructor method with a parameter
public Car(String carColor) {
// parameter value assigned to the field
color = carColor;
}
public static void main(String[] args) {
// program tasks
}
}
Looking on exercises for next lessons ( INHERITANCE AND POLYMORPHISM for example) all constructors use .this:
class Noodle {
double lengthInCentimeters;
double widthInCentimeters;
String shape;
String ingredients;
String texture = "brittle";
Noodle(double lenInCent, double wthInCent, String shp, String ingr) {
this.lengthInCentimeters = lenInCent;
this.widthInCentimeters = wthInCent;
this.shape = shp;
this.ingredients = ingr;
}
}
Can somebode explain that please?
Hi! 
I believe this is a rather basic question, but this is something I can’t figure out. I’m still struggling with the basics, I’m afraid 
Looking at the following code in the exercise:
public class Car {
String color; // constructor method with a parameter
public Car(String carColor) {
// parameter value assigned to the field
color = carColor;
}
public static void main(String[] args) {
// program tasks
}
}
I believe I understand what’s going on. I declare the class Car, and then I create a new instance of the class using the constructor within the class. This class accepts a string variable to define the car color. So, if later, I use the following code:
new Car("blue");
// carColor references "blue" inside constructor
new Car("yellow");
// carColor references "yellow" inside constructor
I can create an instance of Car of blue color or yellow color. So far, so good.
But, what’s the meaning of String color;
right after declaring the public class Car {
? In other words, why do I need to declare a string variable color
and then assign the value of color
to carColor
in the constructor? Why not simply declaring the String color
variable inside the constructor?
This String color
inside the Car class and color = carColor
inside the constructor looks redundant to me.
Many thanks!
1 Like
String color
right after public class Car
is a member variable/field.
scope rules. Declaring a variable within a method makes it local. Then we can’t access the color in other methods
the member variable/field can be accessed in other methods of the class. So we could make a printColor method which would print something like: the color of the car is: [color]
, where [color]
is the member variable/field
Maybe that is a good challenge? Go ahead and implement this method 
There is more to member variables/fields (with inheritance and so forth), but that is for later
4 Likes
I am totally lost as well.
If you have a specific question, you can post it here with any relevant code and someone can help you.
Otherwise, try going over some of the lessons again. Here is an article about constructors with parameters.
Not sure. Java is not a language I use a lot, so not familiar with this specific aspect. Maybe @victoria_dr knows?
1 Like
Java allows you to use the same name for both, but it won’t accomplish what you intend. This will result in a semantic error. Take a look at this example.
public class Pizza {
int diameter; // default value of 0
String cheeseType; // default value of null
public Pizza(int diameter, String cheeseType) {
diameter = diameter;
cheeseType = cheeseType;
}
public static void main(String[] args) {
Pizza order1 = new Pizza(12, "mozzarella");
System.out.println(order1.diameter); // prints 0
System.out.println(order1.cheeseType); // prints null
}
}
As you can see, order1
's instance variables are not assigned the values of 12
and "mozzarella"
, as expected. Rather, they have the default values of 0
and null
.
This is because the constructor doesn’t assign the parameter values to the instance variables. Rather, it sets the parameters equal to themselves.
For a much more in-depth analysis and possible alternatives, see this resource.
2 Likes
public double divide(int a, int b){
return a/b;
From this line of code
When divding 5/4 we get 1.0
Do we have to use double for a, b also?
Yes. When dividing two int
s, the value returned will also be of type int
.
There are several ways to ensure true division, rather than integer division, happens.
- Make
a
a double
.
- Make
b
a double
.
- Make
a
and b
double
s.
- Use type casting (example below). More information here.
Type Casting Example
int a = 5;
int b = 4;
System.out.println((double) a / b); // prints 1.25
In this example, a
is cast to the double
type, which means the expressions a / b
is treated as an int
(b
) divided by a double
(a
). This is similar as if a
was declared as a double
and b
as an int
.
However, note that type casting does not change the type of variable, meaning a
is not changed from an int
to a double
. Rather, the specific occurrence of a
in the expression a / b
is treated as a double
.
product
isn’t the actual string, its a variable/parameter of type string. The actual string comes when you create a new object/class instance
guys I’m totally lost, I feel so stupid