FAQ: Java: Introduction to Classes - Classes: Review

This community-built FAQ covers the “Classes: Review” 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: Review

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!

In the output screen of this review code, I’m not sure why “Constructor Invoked!” is printed 2 times after “Main method started”…is it because two new Dog instances (Fido and Nunzio) are being created? Thanks! 02%20PM|690x498

2 Likes

Yes. Any instance creation calls the constructor, and the constructor is the one that prints “Constructor Invoked!” when it is called.

4 Likes

Hello, might be a stupid question…
Why is it necessary to create additional variables in the constructor and assign them the values of the original ones and not keep refering to the original variables?

4 Likes

I thought the same thing. The reason is you don’t want to change your variables sometimes. So you keep one constant and then you can make separate variables as needed.

2 Likes

There is an error in the code example, the last line fido.breed; when compile, “error: not a statement”.

Maybe it should be: System.out.println(fido.breed);

How to add color to the dogs?

public class Dog {
String breed;
boolean hasOwner;
int age;
String color;

public Dog(String dogBreed, boolean dogOwned, int dogYears, String dogColor) {
System.out.println(“Constructor invoked!”);
breed = dogBreed;
hasOwner = dogOwned;
age = dogYears;
color = dogColor;
}

public static void main(String args) {
System.out.println(“Main method started”);
Dog fido = new Dog(“Poodle”, false, 14, “White”);
Dog nunzio = new Dog(“Shiba inu”, true, 12, “Dark Brown”);
boolean isFidoOlder = fido.age > nunzio.age;
int totalDogYears = nunzio.age + fido.age;
System.out.println("Two dogs created: a " + fido.breed + " and a " + nunzio.breed);
System.out.println("The statement that fido is an older dog is: " + isFidoOlder);
System.out.println("The total age of the dogs is: " + totalDogYears);
System.out.println(fido.breed + "s are " + fido.color + " and " + nunzio.breed + "s are " + nunzio.color + “.”);
System.out.println(“Main method finished”);
}
}

Ah, String​:ok_hand::ok_hand: fml… TY

På torsdag 6. februar 2020, 19:16:33 CET skrev Ramanpreet81 via Codecademy Forums codecademy@discoursemail.com følgende:


ramanpreet81

    February 6

public class Dog {
String breed;
boolean hasOwner;
int age;
String color;

public Dog(String dogBreed, boolean dogOwned, int dogYears, String dogColor) {
System.out.println(“Constructor invoked!”);
breed = dogBreed;
hasOwner = dogOwned;
age = dogYears;
color = dogColor;
}

public static void main(String args) {
System.out.println(“Main method started”);
Dog fido = new Dog(“Poodle”, false, 14, “White”);
Dog nunzio = new Dog(“Shiba inu”, true, 12, “Dark Brown”);
boolean isFidoOlder = fido.age > nunzio.age;
int totalDogYears = nunzio.age + fido.age;
System.out.println("Two dogs created: a " + fido.breed + " and a " + nunzio.breed);
System.out.println("The statement that fido is an older dog is: " + isFidoOlder);
System.out.println("The total age of the dogs is: " + totalDogYears);
System.out.println(fido.breed + "s are " + fido.color + " and " + nunzio.breed + "s are " + nunzio.color + “.”);
System.out.println(“Main method finished”);
}
}

Yes, that is the answer.

Is there any way to prevent this repetition?

At the review stage I commented out the original statements that assigning constructor parameter values to corresponding instance fields. This code trial ran just as before commenting out these variables’ assignments.