oop - Difference between "passing reference after assigning an object" and "passing object using new keyword" to a method in java? -


What does it matter?

We think that Java has a method as follows:

  void demoMethod (MyClass mc) {// some operations}  

The first type:

  demoMethod (new MyClass ()); // Passing an object directly  

The second type:

  MyClass mc = new MyClass (); DemoMethod (MC); // Passing the reference of an object  

Anyone in the context of the behavior of that method The difference is not the reference. The first code can translate into other words as much as possible. Eventually, using the new MyClass () object needs to be stored somewhere so that it can be reloaded and sent to the method.

However, using the second code can reuse the context.


Comments