package com.codegym.task.task02.task0212;
/*
Crazy eights
*/
public class Solution {
public static void main(String[] args) {
Cat cat1 = new Cat();
Cat cat2 = new Cat();
Cat cat3 = new Cat();
Cat cat4 = new Cat();
Cat cat5 = new Cat();
Cat cat6 = new Cat();
Cat cat7 = new Cat();
Cat cat8 = new Cat();
Cat cat9;
Cat cat10 = cat8;
}
public static class Cat {
}
}
Gzzui892
Level 6
I wonder why this is wrong. Cat cat10 = cat8; should only make an object reference variable and not an Object. Thus, the requirement of making 10 variables and 8 objects should be cleared. Or there is a misunderstanding in my part?
Resolved
Comments (6)
- Popular
- New
- Old
You must be signed in to leave a comment
Sela
9 June 2020, 12:03
The 4th of the requirements is not fulfilled. It is stated: "In the main method, 8 variables must be initialized immediately upon creation". In your solution the variable cat10 is also initialized by assigning to it an object which is referenced also by variable cat8. So now we have 9 initialized variables - should be 8. I hope this helps.
0
Gzzui892
9 June 2020, 12:27
Hmm seems like I have issue with the term initialization.
************
In simple occurences, intialization is something like this:
int x; // x is initialized.
int y = 8; // y is initialized and is assigned to a value 8.
int z = y; // z is initialized and is assigend to a valued the same as y.
a = z; // a is not initialized as an integer although it is assigned to an integer z.
*****************
Using the concept initialization as stated above, then Cat cat9; is also initialized since we know what cat9 is, not like 'a' because cat9 is followed after Cat while 'a' has nothing that follows it.
Cat cat9; also assigns the cat9 to the 'null' value.
Using your word "cat10 is also initialized by assigning to it an object which is referenced also by variable cat8" means that the 'initialization' here means as long as it is referenced to an object then it is initialized. Therefore, cat9 is not initialized since it does not reference itself to any object.
I thought that initialization would mean 'create a new object and the corresponding reference to the new object'. Hence, I did not think that just referencing a new object reference variable to an-already-existing-object is an initialization.
************************
If my thinking is correct, then initialization would differ in meaning in some cases?
0
Sela
9 June 2020, 12:55
a variable defined within a method (here main() method), constructor or block is visible only to this block and is not initialized automatically like instance variable. is has to be initialized before use. so if int x; is defined within a method then it is not initialized - only declared. if it is placed directly in class body then it is the instance variable initialized with 0. this is how I understand initialization.
0
Gzzui892
9 June 2020, 16:53
Hi, I just found something that may be of help to clarify some things.
Declaration is not to declare "value" to a variable; it's to declare the type of the variable.
Assignment is simply the storing of a value to a variable.
Initialization is the assignment of a value to a variable at the time of declaration.
So basically "initialization" has two possible definitions, depending on context:
1. In its narrowest form, it's when an assignment is comboed with declaration. It allows, among other things, special array shorthand initializer syntax
2. More generally, it's when an assignment is first made to a variable. It allows, among other things, assignments to a final variable at multiple places.
src: https://stackoverflow.com/questions/2614072/java-define-terms-initialization-declaration-and-assignment
**************************
According to the definition above, then it does make sense why Cat cat10 = cat8; is counted as an initialization.
I hope this can help us to understand things better.
+1
Sela
10 June 2020, 04:17
moreover, if you use not initialized variable within a method like this:
then the compiler will complain "...variable cat9 might not have been initialized". +1
Gzzui892
10 June 2020, 05:17
Yes, in this case the Cat cat9; is only declared and therefore there's nothing that can be printed out.
+1