in this code.... public static class Cat { public Woman owner;
} ... a variable 'owner' is declared with Woman data type .
but i have seen var declarations as like - int rate or char name and these storing integer and character value. . .. how can we declare a variable with Woman data type and what value will it store ??
help in { public Woman owner; }
Resolved
Comments (14)
- Popular
- New
- Old
You must be signed in to leave a comment
Nouser
13 February 2021, 14:14solution
declaring a varibale of the type Woman usually is done similar to
;
in your code example
the instance variable owner stores a reference to a Woman object. By default instance variables are initialized with null +2
Shubham Yadav
14 February 2021, 13:39
in the first code lets take -
Woman obj = new Woman();
so here ' obj ' and ' owner ' from the second code have same function !?
Both are acting as a reference to class object !?
0
Gellert Varga
14 February 2021, 14:29
Yes.
' obj ' or 'owner' are just names of a variable.
You can give such a name to a variable what you want.
0
Shubham Yadav
14 February 2021, 20:32
I want to know that both are having similar function ?
, as both are acting as refernce to the object of Woman class.
0
Gellert Varga
14 February 2021, 20:59useful
If you need one Woman-object in your program, no matter how do you named its variable, it will do the same function.
or
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Other example:
The woman1 and bella and stella : All three of these store a reference to an object created from the Woman class. But not for the same object! These store references to three different Woman-objects. +1
Shubham Yadav
15 February 2021, 06:30
and the 'onwer' is same like wmn,bella,stella,woman1 . !?
0
Gellert Varga
15 February 2021, 11:04
Probably yes!
But I can only give a definite clear answer if you send me the specific program code, so if I will see the correlations in it.
0
Shubham Yadav
15 February 2021, 14:07
its from level 2 lesson 5
ques - pets need people
0
Gellert Varga
15 February 2021, 15:04
In an extreme way I like the punctuality/exactness.
Two people can extremely easy to misunderstand each other, especially if their native language is not the same. (As you and me.)
That's why I wrote a similar program here, please ask what the exact question is based on the exact names used in it! Please refers to the line of the code also, thanks.
0
Shubham Yadav
16 February 2021, 13:47
1.--> from line 15 the variable 'owner' is acting as ref. to object of Woman class and from line 7 and 8 the wmn and bella are also acting as ref. to object of Woman class.
So ques is that 'owner' has same functionality as that of 'wmn' and 'bella' ?
2.--> from line 10 , with kitty as ref. to object of Cat class ، the var owner has been given value of wmn. and the owner is itself a ref. to Woman(class) object.
then also a new reference wmn is assigned to it.
So here why are we doing this and what will be benefit of doing this ? I'm not able to get this .
sorry for giving u this much trouble.
0
Gellert Varga
16 February 2021, 15:16useful
Hi!
No problem!!:)
I like to explain things - but only if i can understand the real question... (And i like (and must) to practice the English, too.)
I think now I can understand your problem, and i hope i will be able to make it clear.
Line 14:
public static class Cat {
public Woman owner;
}
It's just a class. It doesn't do anything in itself. It doesn't refer to any object.
This class is just a blueprint/pattern for the future Cat-objects.
This class just says this:
"If you want to create a Cat-object in the future, then each of the Cat-objects:
- will have only one own variable,
- this variable will be named: "owner",
- and the type of this variable will be "Woman"-type. (So, this variable "owner" can store only a reference to a Woman-object, nothing else.)"
Only the created Cat-objects will have the variable "owner".
This "owner" variable does not exist in itself!
But each created Cat-objects will have an own "owner" variable!
Line 18.19: this is also just a blueprint, for the future Woman-type object.
Line 4, 5: Here we created two Cat-type objects.
One of them is placed into the reference-variable "kitty",
and the other Cat-object is placed into the reference-variable "cat2".
Line 7, 8: Here we created two Woman-type object.
One of them is placed into the reference-variable "wmn",
and the other Woman-object is placed into the reference-variable "bella".
Woman wmn = new Woman(); // it means:
we declare a Woman-type wmn-named variable, and we assign to it a newly created Woman-type object.
After all of these, but still before line 10: we have two Cat-objects, both of them have an own "owner" variable, but till now we didn't give any values to them. Therefore:
Line 10: we take the one of the Cat-objects, named kitty, and we give a starter value to its "owner" variable:
we place one of the earlier created Woman-type objects, into the "owner"-variable of kitty, this way:
kitty.owner = wmn;
+1
Shubham Yadav
17 February 2021, 14:11
I really appreciate your help. Thank you.
one last thing , I got all what happened in the code , but I don't get why all was done , so do I have to dig deeper in this to know why this was done or will I get that in upcoming chapters.?
0
Gellert Varga
17 February 2021, 15:45
As you progress through this CodeGym course, everything slowly becomes clearer.
In Java we work with objects.
This little program is a little tutorial on how to model reality in a program.
+2
Shubham Yadav
19 February 2021, 15:13
thank you..
0