The husband and wife part I wouldn't be able to understand myself. I have two questions:
Q1. Is woman.husband = man; the same as:
public static class Woman {
public Man husband = man;
}
Q2. Will I learn more about this later?
A bit of an explanation?
Under discussion
Comments (2)
- Popular
- New
- Old
You must be signed in to leave a comment
Guadalupe Gagnon
6 May 2021, 18:12
The answer to question 2 is yes, you will learn this and more as you go along.
You will be confused about a lot of programming concepts if you are brand new to programming, that is completely normal. You are also going to feel like you are behind with learning with each additional level, which is also very normal. The thing is, if you want to become a programmer, you have to keep moving along.
When you are level 10, most of these tasks will be able to solve without even thinking about it, but those level 10 tasks will stump you. When you are level 15 you will solve the level 10 tasks in 2-5 minutes, while staring at the screen for over an hour on the level 15 tasks. Same thing when you reach level 20, 30, 40.... and 5 years into your career.
Watch the first video I shared on this post: Lupe's Forum Post
I highly recommend ALL people new to programming watch this video.
+3
Guadalupe Gagnon
6 May 2021, 18:21
Now for your first question, it looks like you have the correct idea, however this is not valid code:
the variable "man" here is not declared and can't be used in the block of code. A class is only a blueprint on how to create an object. A common analogy is like a house blueprint, it shows how to build a house, but it isn't a house. Think of a class as the same way: its a blueprint, not a house. In java an object is actually constructed using the 'new' keyword.
While that code is not valid, I think you are just trying to assert your understanding of what the line woman.husband = man; is doing. In that sense you are correct in that it is setting the value of the "Man husband" of the woman object created. It is important to understand that it is setting the object's husband field and not the class field. Just like a house blue print can be used to build multiple houses, a class blue print can be used to build multiple object:
Here only the variable "woman1" has its husband field set to "man", "woman2" and "woman3" still have null values (non-existent) values for that field. That is like if you build 3 houses from the same blueprint, then paint one of the houses red. The other two houses will not be red too.
Anyways, this is a complicated subject and Java books devote whole chapters to just this. You will get it in bits and pieces as you learn more, and solve more tasks. Watch the video I shared above. +4