Returning Objects

Shubham Dadasaheb Sawant
1 min readJun 14, 2022
  1. Till now we returned primitive data types now lets try to return objects from methods.

OUTPUT:
2
20

2. while returning the object that you created in fun1() , function return type
must be class name.
3. In main method first we created the object and later we printed the value of
a from that object , later we called fun1() with reference to obj1 and in fun1() we created object manipulated that objects value and returned the object.

NOTE:

Since all objects are dynamically allocated using keyword new, you do not need to worry about an object going out of scope because the method in which it was created terminates. The object will continue to stay as long as there is reference to it somewhere in the code. When there are no references to it, object will be reclaimed the next time garbage collection operation takes place.

Happy Coding !!

--

--