Hello everyone , i managed to complete this task for the level 2 :
public class Solution {
public static void main(String[] args) {
//write your code here
Cat cat1 = new Cat();
Woman woman1 = new Woman();
cat1.owner = woman1; // i don't get well the meaning of this
}
}
public static class Cat {
public Woman owner; // and expecially this
}
but somehow i'm not quite sure if i understood the functionality of this in Java, creating an object alright, assigning the object to istance variables okay, but in this case what should this be? Assigning a reference to another reference?
I don't know if i explained myself well , but could you explain this to me better ? Or maybe do you have some internet sources that explain this in a more detailed way?
Thanks a lot!
Question about taks level 2 - Assigning reference variables
Resolved
Comments (3)
- Popular
- New
- Old
You must be signed in to leave a comment
Gellert Varga
4 April 2021, 14:13solution
This is a class. That in itself does nothing. This is just a blueprint for later cat objects.
It means each created cat object will have the following variables:
age (primitive, int),
and owner (reference type: Woman-type)
When you write this:
then you created a new Cat-type object.
This cat1 object has an int age variable and a Woman-type owner variable.
But you have not yet assigned a specific value to any of the variables.
You can assign a number to the age variable:
And you can assign only a previously created Woman object's reference to this Woman-type owner variable:
In this case the ladyGaga and the cat1.owner variables have the same reference to the same Woman object.
+9
Luniae
4 April 2021, 14:30
Thanks a lot , now is clear. And to be able to assign the Woman owner reference to the Cat class i have to respectively create another Woman class that act as a blueprint for the Woman object, right?
public class Woman {
}
0
Gellert Varga
4 April 2021, 20:19
Yes.
1) You have to have a Woman class.
2) You have to create an object from this Woman class, and to store its reference in a Woman-type variable.
3) You have to create an object from Cat class, and you have to store its reference in a Cat-type variable
4) Now you can assign the reference from the point 2 to the cat1.owner.
+1