50 - Orientação Objetos - Métodos pt 07 - Parâmetros tipo referência pt 02
Understanding Parameter Passing by Reference
Introduction to Parameter Passing
- The discussion begins with an introduction to the topic of parameter passing in programming, specifically focusing on passing parameters by reference.
- The speaker references a previous lesson where they discussed how objects are passed in programming, emphasizing that when passing by reference, the actual reference to the object is sent rather than a copy.
Implications of Passing by Reference
- It is highlighted that changes made to an object through one reference will affect all references pointing to that object, which can lead to unintended consequences if not managed carefully.
- An example is provided where two student objects (student 01 and student 02) are manipulated within a method, demonstrating how altering one affects the other due to shared references.
Demonstrating Object Modification
- The speaker modifies properties of student objects within a print method, showing how these changes reflect across different instances when printed.
- A specific change is made to the name property of student 01, illustrating that modifications persist beyond the scope of the method call.
Complications Arising from Shared References
- The order of operations in code execution is altered, leading to further exploration of how shared references impact output during multiple calls.
- When printing both students after modifications, it becomes evident that changes made earlier affect subsequent outputs due to retained references.
Best Practices for Managing Object States
- The speaker emphasizes best practices in programming: avoid altering data directly on passed objects. Instead, create new instances or return modified copies.