Found this lession the most intresting really enjoying this!
Michael AmannLevel 22, United States of America, United States
13 April 2022
The instructions say 4 parameters are specified: left, top, width, height
Then it says..."height is not specified (it is equal to the width)" Height is one of the parameters. So which one is it?
Justin SmithLevel 41, Greenfield, USA, United States
5 July 2021
There's a hidden lesson to notice with "Initializing Cats". if you look at what you have for the 5 initialize methods, you'll notice that they all have a unique sequence of variable types. They are, in order:
string
string, int, int
string, int
int, string
int, string, string
This is because this is how it knows which initialize method you are calling. If you had two initialize methods that both took a string and an int in that order, then when you called the initialize method and had the string and int arguments, it wouldn't know which one you were calling.
Well, while that is true, the compiler wont let you make a mistake that would not enable it to choose. try compiling a class with two such methods and you would get java: method initialize(java.lang.String,int) is already defined in class
I'm not sure I understand: initialize method is another way to given variable to object?
Previously we did:
Person person = new Person();
person.name = "Mark"; etc.
or with getName
and now we make initialize. Where is difference?
They are virtually the same with the fact that they help you assign values to the variables, but I think the initializers are bigger than that. The way they are described here, they are supposedly a way to make the class useful. I think they are even bigger than the concept of setters which, currently, they look like one. To understand what I mean, I am going to use an example used in some previous articles they posted here:
Imagine a class named Cat with variables String name, and int age. Virtually, you can do this:
Cat cat = new Cat();
cat.age = -1000
However, making a cat that is -1000 years old doesn't make sense, hence you are to make setters:
private int age;
public void setAge(int age) {
if (age > 0)
this.age = age
else
System.out.println("That age is impossible!")
}
So here, we created a setter function setAge, which is used so that we could set age according to proper conditions. Now, we also set the int age variable to private so that it cannot be accessed outside the class if not through the setAge method. This basically guarantees they use the setAge method else there's really no point if they can edit the value beyond the setter.
With this in mind, we can imagine that if multiple variables have required rules, you can have multiple setter methods, and then one initializer method where you can collate all of them. Of course, at this level, only setters makes sense to help initialize the class, but I think there are more to come. If so, these methods can also be included in the initializer method.
"...the mysterious and beautiful creatures we call cats." - CodeGym
Hey. After I read one book they were not as "mysterious" as they once were, and for many other people too. But they certainly are BEAUTIFUL-- or, rather, CUTE. - MV Maryem V
So there cannot be 2 initialize methods in the class with the same vairable type (e.g. int) but for different fields ? e.g.
private void initialize (int top){
}
and
private void initialize (int left){
}
Exactly. The compiler doesn't know how to tell the two apart because they both pass the same type of value. However, the below code would still work because it matters what types of values are being passed, not what the actual values are.
I just learned this the hard way; I worked way harder than I needed to. I though there would be lots of intialize methods and I didn't want to type them all out. So I wrote a python script to generate what I though was all possible initializers. I didn't realize that java can't tell the different between the following:
public void initialize(int left, int top) {
this.left = left;
this.top = top;
}
and
public void initialize(int left, int width) {
this.left = left;
this.width = width;
}
So I generated 12 initialize methods when only four were possible.
Oh well, it was a good learning exercise.
GO TO FULL VERSION
-CodeGym Hey. After I read one book they were not as "mysterious" as they once were, and for many other people too. But they certainly are BEAUTIFUL-- or, rather, CUTE. - MV Maryem V