Codecademy Forums

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.