FAQ: Java: Introduction to Classes - Classes: Assigning Values to Instance Fields

This community-built FAQ covers the “Classes: Assigning Values to Instance Fields” 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: Assigning Values to 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!

Why are instance fields required? In the example it sets the value of color to be the value of the parameter carColor. Then, when passing the values during method call, the value references to the constructor parameter then to the instance field from what I understood.

Doesn’t the constructor parameter already says what is the data type and name of the object? Why its value needs to be set equal to an instance field?

I’m also confused by this, not sure why it is necessary and when changed some code around it returned null or with an error.
public class Car {
String color;
//We created a class, Car with a variable of color.
public Car(String carColor) { //Why is this not public Car(String color)?
// assign parameter value to instance field
color = carColor; //Why dis we assign the same variable to two different names?
}

here:

String color;

you declare the instance field.

then in the constructor:

public Car(String carColor) {
    // assign parameter value to instance field
    color = carColor;
  }

the comment pretty much says it, we assign the parameter (which is a placeholder) to the instance field

then once we initialize the class:

Car ferrari = new Car("red");

we provide the car color (red) to the constructor.

could we use the same name for the instance field and the constructor parameter? Yes. But its important to understand how it work.

1 Like

I appreciate your reply but not sure you answered my question. Why does the code not look like this?

public class Car {
String color;

public Car(String color)
}

I’ve tried to find out the answer but I think I’m misunderstanding something. I’ve also seen them use.

public Car{
String color;
}

public Car(String color){
this.color = color;
}

I’m confused why you need either. I appreciate your feedback.

Let me explain my question a bit more.

public class Car {
String color;
boolean isRunning;
int velocity;
This code is creating a class, Car, which has 3 parameters.

public Car(String carColor, boolean carRunning, int milesPerHour) {
color = carColor;

What is this doing if we’ve already created a class Car, with 3 parameters? I don’t understand what the difference between these two are.

it seems you confuse instance field (String color;) and the parameter of the constructor (String carColor).

the parameter of the constructor (carColor) gets its value from the argument we provide when instantiate the class:

 Car ferrari = new Car("red");

the argument ("red") is passed along to the parameter of the constructor (carColor). Its important to understand that creating a class instance automatically calls the constructor.

the advantage of this approach is that if we make a new car, we can give a different color:

public class Car {
  String color;

  public Car(String carColor) {
    // assign parameter value to instance field
    color = carColor;
  }

  public static void main(String[] args) {
    // parameter value supplied when calling constructor
    Car ferrari = new Car("red");
    Car mcClaren = new Car("orange");
    System.out.println(ferrari.color);
    System.out.println(mcClaren.color);
  }
}
1 Like

classes do not have parameters, methods do. color, isRunning and velocity are instance fields.

the constructor is called when you instantiate the class, allowing us to pass different values for each car that we make.

do you understand when the constructor is invoked?

1 Like

I REALLY appreciate the time you are taking to answer my questions. It just seems like a foreign concept. Maybe I just need to accept it as a requirement of the language instead of asking why.

I want to thank you again for your feedback. You have been a great help.

This is why Java is not a great first language, seeing the value of OOP when started programming is really difficult.

What is it your are struggling with, i feel like its here:

public Car(String carColor) {
    // assign parameter value to instance field
    color = carColor;
  }
Car ferrari = new Car("red");

doing new Car("red") will invoke the constructor public Car(String carColor), which results in:

carColor = "red" # assign value of instantiate class to parameter of constructor 

given carColor is now red, it will set the instance field (color) to red.

now that the instance field is set, we can use it on the class instance:

System.out.println(ferrari.color);
1 Like

Ok, I understand now. I didn’t realize the println method used .color instead of .carColor.

You mention Java being a difficult first language. I am not too far invested to stop and start on another language, especially if another language will give me a better base to work from.

Do you have any recommendations?

carColor has a local scope, it only exists within the constructor. Which is why we assign it to the instance field, then we can use the instance field (color) on the class instance (ferrari)

Java relies so heavily on OOP, which can be very overwhelming in the beginning. And difficult to grasp what the advantages and drawbacks of object oriented programming (OOP) are.

which is why i think its not a suitable first language, but ask a different developer, he might have a different opinion. Some even go so far to say you should start with a teaching language. (purely for teaching, not or barely used in the industry)

this is just an opinion, not a fact. Python is a better option i think.

although python is OOP as well, you can start simple with functions:

def start(name):
   print "hello " + name

start("codecoder")

without all the classes and other luggage. Of course, python has its flaws and problems as well. You can also stick with Java if you want

1 Like

Again, I really appreciate your help and feedback.

I will take a look at python as you are not the first to say it is an easier first language.

Thanks again, I hope you enjoy the rest of your weekend.

Wouldn’t you know, Scope is the topic 2 pages after the chapter that caused so much confusion for me. Might be a good idea to place “Scope” a bit earlier to avoid confusion.

I am just helping people on the forum, i have no say in the lesson structure and so forth

sometimes you just have to go through and you will pick things up you missed earlier (like scope)