CodeGym /Java Course /Java Syntax /Practice with constructors

Practice with constructors

Java Syntax
Level 5 , Lesson 9
Available

"I can see that you've already rested. Great! Here are some tasks to practice creating constructors:"

5
Task
New Java Syntax, level 5, lesson 9
Locked
Nerd break
Did you know that before the final power lift and transition to Level 5, you need to relax a bit and watch a video from the "Did You Know?" series. This isn't just entertainment. It's part of the comprehensive curriculum developed by the pedagogical board at the secret CodeGym center. The video is dedicated to the technologies and progress achieved by mankind.
Comments (58)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
MoJo Level 23, Cairns, Australia Expert
9 January 2024
Okay, once you completed these tasks try Alt + Insert in IntelliJ. You are welcome ^^
Mindre Mercedes Bautista Level 6, Rome, Italy
21 March 2022
estoy confundida. que es esto? public Color color; no es una variable porque no tiene el tipo. no es un constructor porque le falta (){ Alguien me puede decir que es: gracias...
Herny78 Level 6, San Miguel, Argentina
29 March 2022
public Color color; (crea variable de la calse Color) y esta variable se inicializa en el paso 17 cuando se llama al constructor de la clase Cicle,
Xavi Martin Level 22 Expert
4 October 2022
public Color color es una variable. Es una variable del tipo Color. Es una variable de la clase Color. Al igual que String color seria una variable del tipo String, Color color seria una variable del tipo Color.
Stanislav Mayer Level 15, Czech Republic
16 January 2022
I don't think the "Constructor" task is "Medium": it is more likely "Hard" or even "Insane" :-) Btw. if you are, like me, puzzled with two classes within the same class, this is called "nested classes" and this topic is explained later in "Java Multithreading" course (level 20+). Or you can peep at this article: https://codegym.cc/groups/posts/262-nested-inner-classes
0wis Level 6, Paris, France
13 December 2021
Best exercise was calling a constructor with another constructor. So "this" is also a method that can be called. I feel like I just begin to understand the power of this...
Aldo Luna Bueno Level 28, Peru
15 November 2021
El ejercicio «Constructor» me pareció algodifícil de ver. Debería ser de difícultad media o mayor. Muy aparte de eso, qué gran ejercicio; me hizo pensar bastante.
Dave Level 6, Saskatoon, Canada
7 July 2021
Task 22: Max Constructors: What about constructing a Circle object from another Circle object? Doesn't that make 5 possible constructors?
Mrrobot993 Level 9, Italy
18 October 2021
No, one for all parameters, one for 2 parameters, one for 1 parameter and the default constructor.
Jonaskinny Level 25, Redondo Beach, United States
11 February 2022
Yes Dave ... I had 5 including default (since we provide non-default we need to manually add back the default if we want max) and using Circle as parameter. Make sure to check if null != Circle param before using circle.attribute
Justin Smith Level 41, Greenfield, USA, United States
6 July 2021
Found something that I would consider a bug in "Constructor". If you change the sole class variable from public to private (we were taught previously that they should be private and only accessed by getter and setter methods), it doesn't affect the ability of the program to run as intended, but it will fail validation for that reason alone.
Justin Smith Level 41, Greenfield, USA, United States
6 July 2021
It may seem super-tedious to repeat the exact same tasks we did with initialize, but the point is to make some of these processes automatic for the learner.
envy Level 6, Detroit, United States
11 February 2021
I found the bug in this problem. But I have a basic question. Arent we to initialize the object with something like

this.color = color

public class Circle {
    public Color color;

    public static void main(String[] args) {
        Circle circle = new Circle();
        circle.color.setDescription("Red");
        System.out.println(circle.color.getDescription());
    }

    public void Circle() {
        color = new Color();
    }

    public class Color {
        String description;

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }
    }
}
I am not clear how this initializing the object with a new works inside a constructor. Can someone please help clarify ?
Juanf Level 26
11 February 2021
Could be done also the way you say, if the constructor of Circle class would take any parameter (in that case the parameter should be "Color" class). The actual constructor of Circle class doesnt´take any Color parameter, but directly creates a new instance of Color and asigns it to color variable of Circle object, it is only left to "setDescription" of the color object.
P.B.Kalyan Krishna Level 24, Guntur, India
1 March 2022
I am surprised with the code in line 10; public void Circle(){ color = new Color(); } Is this a method or a constructor? Technically Java allows a method declaration with the same name as the class name, but this privilege is reserved to constructors. Constructors are declared with the same name as that of the class name, and they don't have a return type, not even void. So the above code must be treated as a method, not a constructor. This is a bad practice ie naming the methods with the class name and should be discouraged.
Zach Level 22, Fort Collins, United States
2 February 2021
I have been using a different naming convention to help differentiate the parameters. Is this considered bad form and if so is there a better way to do it. Thanks in advance. EXAMPLE.

    public Cat(int _weight, String _color, String _address) 
    {
        this.weight = _weight;
        this.color = _color;
        this.address = _address;
    }
Naughtless Level 41, Olympus Mons, Mars
6 May 2021
If you're going to be using different names on the parameter, you don't need "this.".

public Cat(int _weight, String _color, String _address)
{
    weight = _weight;
    color = _color;
    address = _address;
}
schipplock Level 12, Germany
10 July 2021
For me this is bad code. But it depends on where you are working :). I've seen crazy rules.