W
W
wusleng2020-04-13 15:21:25
Java
wusleng, 2020-04-13 15:21:25

How does this piece of code work?

I created the variable Man(variable type) man(Variable name) = new Man()(Type of the Created Object).
How does this code work?

public static void main(String[] args) {
        Man man = new Man();
        Woman woman = new Woman();
        woman.husband = man;
        man.wife = woman;
        
    }

    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;
    }


Explain more precisely how
woman.husband = man;
man.wife = woman;

with classes Man and Woman.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dex16, 2020-04-13
@wusleng

woman stores a reference to man, from the woman class you can access the fields of the man class.
For the man class, the same is true.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question