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

1 Like

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.

2 Likes

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);

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.

3 Likes

I have the same question as beta3611722127 above. And reading the replies still didn’t answer the question. Or maybe I just fail to understand.

Why do you have to declare the instance field outside the constructor method? Why can’t you declare and assign the value inside the constructor method to make the code shorter/simpler. like this for example

I know it will cause an error, but the code will be easier to understand if you could declare it that way.
I am guessing that it is because you also need those instance fields in main() method too?

You don’t. The errors you are getting are because you didn’t declare the data type of the variable.

1 Like

oh wow, completely missed that part.
I will try it again.

So, I tried it again and yes, you could declare the instance field inside the constructor method.
But when I tried to call System.out.println(cookieShop.productType); inside the main() method, it gives error.

Why is the error happen? Is it because I didn’t declare the instance field outside constructor method, so main() method could not call the instance variable?

EDIT: nvm, it explained on the next lesson.

1 Like

Yes, my bad. You do need to declare the variables in the class before the constructor, but you don’t need to initialize (assign a value) to those variables.

1 Like

OFF TOPIC Why is this car not running, but still going 70mph?

Why won’t this code work?

Welcome back to the forums!

The error indicates that .count is not a property of cookieShop. What change do you need to make?

Should i be printing Store.count?

Take a look at the code inside your Store class, outside of the main method. What instance variables have you declared? Hint: productType is an instance variable, can you find the other ones? With this in mind, what should you be printing? I also recommend you go back to the beginning of the Classes module and start again.

After going more into classes, I’ve found my mistake. Thanks alot!

1 Like

image
Hello. Regarding variables and parameters. Why is it that the variables can’t just be the same as the parameters for the constructor method?

hey can I develop apps by just learning java ?? please tell me what languages I have to learn to become an app developer.