-
Java Basic (6) - Objects containing objects, Passing an object to a methodJava 2021. 5. 20. 00:17
Objects containing objects
- Object can contain any type including the object type (itself)
Person object contains a type of itself
Passing an object to a method
- Java always passes a copy of the variable to a method, not the variable itself
- When passing a primitive type, a copy of the value is passed to the method
- When passing an object, a copy of the reference is passed to the method
x is just a copy of the value that was passed to the method x in main is still 5 Let's apply this to objects
Modifying where it point to does not affect the initial reference that was passed to the method. However, p here is still accessing the same object in memory, so calling an instance method will affect the object. haveBirthday method
'Java' 카테고리의 다른 글
Java Basic (8) - Exceptions (0) 2021.05.22 Java Basic (7) - Multidimensional Arrays (0) 2021.05.20 Java Basic (5) - Object, clone(), copy (0) 2021.05.19 Java Basic (4) - public, private, encapsulation, accessor/mutator, get/set, static (0) 2021.05.16 Java Basic (3) - toString(), constructor (0) 2021.05.15