FAQ: Java: Introduction to Classes - Classes: Multiple Fields

This community-built FAQ covers the “Classes: Multiple 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: Multiple 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 do we need to declare two different variables, first outside the class and then inside the class, and then equate them in the class, to represent the same data?
Why not declare them at once directly in the class parameter declaration?
Refer to this example : Capture2

The thing is, you can do whatever you want in a constructor, just like so far we did whatever we wanted in the main() method. Remember when in the constructor, we did a System.out.println(“something”)?

So far, all the examples we’ve used, we made the constructor so that it takes as many parameters as the class has fields, and it puts these parameters in these fields.

It sounds rather logical, and it’s actually common. But that is not at all an obligation.

For example, you might not want to have to set all the fields at once when you create a new object. Maybe you’ll set them later, not with the constructor. Or maybe the constructor will just give them a value by itself without taking a parameter, like, the constructor says that the price is 2.00, and it doesn’t take a price parameter.

You can do pretty much what you want. Giving a value of all the fields of the class, will not always be what you want to do. Therefore, the language must not assume that it is what you want to do. Therefore, if it is what you want to do, you must write it out entirely.

1 Like

The first declarations are defining the characteristics of the Car class. A Car has a color, is running or not, and has a velocity. Even though Car is defined, there is no Car object created yet.

The constructor is called when an instance of the Car class is created. The constructor definition takes the parameters passed to it and assigns them to the variables in this particular Car object. You can create multiple Car objects, each with different colors, isRunning values, and velocities like this:

Car blueCar = new Car(“Blue”, true, 34.5);
Car redCar = new Car(“Red”, true, 90.0);
Car junkCar = new Car(“Rust”, false, 0.0);

1 Like

Hello. This question is in regards to Classes Multiple fields.
I completed the task and I got stuck. I selected to reveal the solution which to my surprise seemed exactly the same as my input. I then redid it and screenshotted to compare. And I can still not see any difference. What am I missing?

1 Like

Hi. I think you need to capitalize the S on Shop.

1 Like

Why don’t we use the constructor parameters name same as the variable field names?? I think it would be a lot more readable and understandable rather than using different names for the same thing.

1 Like