public class Solution {
public static void main(String[] args) {
//write your code here
Man man = new Man();
Woman woman = new Woman();
man.wife = husband;
woman.husband = man;
}
public static class Man {
public int age;
public int height;
public Woman wife;
}
public static class Woman {
public int age;
public int height;
public Man husband;
}
}
Not getting line 6 & 7
Under discussion
Comments (7)
- Popular
- New
- Old
You must be signed in to leave a comment
remote87
6 January 2021, 14:06
man.wife = woman;
I think that is the problem
0
Dinesh
6 January 2021, 14:11
i think it should be Man.wife=woman;
But if so I am not getting the code
0
remote87
6 January 2021, 14:14
You have a class Man ( capital M ). The object of that class is man ( with lower case m ). In the class Man you have a field of class Woman, which is called wife, so man.wife = woman ( the object woman of the class Woman ), try it
0
Dinesh
7 January 2021, 05:09
//Kindly help me understanding the code. I am not getting it. Its confusing. Please elaborate if possible programmatically. Thank You.
0
remote87
7 January 2021, 09:41
Ok, I will do my best, don't judge me too hard, I'm not a programmer...yet :D
So, you create a class, a blueprint for an object. Think of it like the instructions to build a car in a factory, but you still don't have the object car ( it's still not build by the robots ). So, in the instructions you have fields ( color of the car, how fast can it goes, how many doors should it has and so on ) and methods ( car drive, honk, stop, reverse and so on ). The class contains all those things, on which you build your object - the instance of the class.
In our case: we have two classes - a Man and a Woman, they both have 3 fields: age, height and wife / husband ( depending on what object we have ).
Next step is creating our objects ( our cars ), following the instructions of the class:
Great! Now we need to set some fields with values:
Fields can be primitive data types ( int, double, boolean and so on ), but they can also be reference type as well ( objects ) and that's what we have. The Man class has a field of an object Woman, called wife ( same as age and height ). We need to set the new object ( man ) fields. With the "." ( dot ) operator we can access those fields, so:
is the same as:
The same logic is applied to the Woman class and setting it's field human to husband. Did you understood my explanation?
Did it pass the system? I did not test it, I just answered you previous question.
Also, this code should output the address of the objects in the heap memory:
0
Dinesh
13 January 2021, 12:09
Dear remote87, It might annoy you.
But asking you this NOT being as JUDGEMENTAL BUT BEING INQUISTIVE
AS IAM NOT GETTING THE STUFF
Again I got stuck in the following code.
0
Nouser
13 January 2021, 13:28
Have a look at that code:
We create a Person object inside the cats constructor call (and do not save a reference to it).
Now a reference to that object is saved in the Cat field owner. If we have a Cat reference var, then we can access that saved Person reference variable using the cat object kitten to get the owner reference variable.. kitten.owner ( <- Person reference variable) and on that Person reference variable we can do everything that's valid for Person objects, like getting the name of the Owner: kitten.owner.name.
That's how you can model a new object consisting of other objects. Really neat, huh:) 0