I managed to get the solution right, but there are things about the code I don't understand. For example, the line: public Woman wife;
Is that simply declaring a new variable of type Woman? If so, could this and the code from the solution be combined as: public Woman wife = woman; ?
And why in the creating of the class variables for Man and Woman classes does it need to have public used?
public Woman wife;
Under discussion
Comments (2)
- Popular
- New
- Old
You must be signed in to leave a comment
Khurram
7 December 2018, 06:04
the objects are created at run time, if you write public Woman wife = woman, at compile time, the compiler will not have any idea about the Woman object named woman.
0
Guadalupe Gagnon
5 December 2018, 16:59
Is that simply declaring a new variable of type Woman? Yes. This declares a public field in the class Man to presumably save a reference to the person who is his wife.
If so, could this and the code from the solution be combined as: public Woman wife = woman; ? The code wont compile this way because woman would be out of scope to the Man class because it is declared in the main method. I don't know where exactly you are in learning, but the topic of variable scope is discussed in the lessons somewhere along the line.
And why in the creating of the class variables for Man and Woman classes does it need to have public used? None of the fields have to be public. This is an access modifier (which will be discussed further along in the lessons). There are different access modifiers that can be used to restrict access to fields to protect the intended use of the class from being modified illegally (meaning the modification could potentially crash the program).
These are good questions. Have faith that your questions will be answered in later lessons. Good luck on your studies!
0