Hello, it seems that something is missing from the lectures. Currently, I am in Primitive data types, Java Syntax, Level 2, Lesson 2. It does not seem to explain the usage of new neither that, cat.owner.name = "God"; Please explain the above and the following code. - Thanks in advance! Java code Description String s; String s = null; Equivalent statements. Person person; person = new Person(); person = null; We create a person variable whose value is null. We assign to it the address of a newly created Person object. We assign null to the variable. Cat cat = new Cat(); cat.owner = new Person(); cat.owner.name = "God"; We create a Cat object and store its address in variable cat; cat.owner equals null. We set cat.owner equal to the address of a newly created Person object. cat.owner.name still equals null. We set cat.owner.name equal to "God"
Primitive Data Types
You must be signed in to leave a comment
5 August 2020, 15:30
Thanks for the reply. However I still do not understand the following lines of code. cat.owner = new Person(); cat.owner.name = "God"; What cat.owner does and why cat.owner.name is equal to that value? Please tell me how this is called, so I can search it on internet.
2 August 2020, 16:10
If i'm understanding your question properly, the "new" in those lines of code is what creates the object, and shows that its a new instance of that object. so Cat cat = new Cat() the first cat calls the class Cat the second is the variable the new object will be associated wit (cat) and the new creates the object Cat() is the object, now bound to the variable cat.