Practice with constructors
Comments (58)
- Popular
- New
- Old
You must be signed in to leave a comment
MoJoExpert
9 January, 16:10
Okay, once you completed these tasks try Alt + Insert in IntelliJ. You are welcome ^^
0
Mindre Mercedes Bautista
21 March 2022, 08:41
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...
0
Herny78
29 March 2022, 21:17
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,
+1
Xavi MartinExpert
4 October 2022, 12:42
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.
0
Stanislav Mayer
16 January 2022, 14:56
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
0
0wis
13 December 2021, 18:34
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...
+3
Aldo Luna Bueno
15 November 2021, 05:14
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.
0
Dave
7 July 2021, 21:48
Task 22: Max Constructors: What about constructing a Circle object from another Circle object? Doesn't that make 5 possible constructors?
+1
Mrrobot993 Full Stack Developer
18 October 2021, 10:04
No, one for all parameters, one for 2 parameters, one for 1 parameter and the default constructor.
0
Jonaskinny Java Developer at Sandmedia
11 February 2022, 17:33
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
0
Justin Smith
6 July 2021, 01:30
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.
0
Justin Smith
6 July 2021, 00:55
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.
0
envy
11 February 2021, 07:07
I found the bug in this problem. But I have a basic question. Arent we to initialize the object with something like
I am not clear how this initializing the object with a new works inside a constructor. Can someone please help clarify ? 0
Juanf Software Developer at EPAM
11 February 2021, 13:52
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.
0
P.B.Kalyan Krishna
1 March 2022, 12:13
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.
+1
Zach
2 February 2021, 21:58
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.
+1
Naughtless
6 May 2021, 11:52
If you're going to be using different names on the parameter, you don't need "this.".
+1
schipplock Backend Developer at n/a
10 July 2021, 20:48
For me this is bad code. But it depends on where you are working :). I've seen crazy rules.
0