<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;"><strong>Java is pass by value</strong> and <strong>not pass by reference</strong> i.e. Java copies and passes the reference by value, not the object. Thus, method manipulation will alter the objects, since the references point to the original objects. But if you change the reference inside method, original reference will not get change. If it was pass by reference, then it would have got changed also. Well, primitive types are always pass by value without any confusion. But, the concept should be understood in context of method parameter of custom types.Let&#8217;s discuss with one example as below.</p>
<pre class="highlight">public class Bar 
{ 
 private String property; 
 
 public Bar (String property){ 
 this.property = property; 
 } 
 public String getProperty() { 
 return property; 
 } 
 public void setProperty(String property) { 
 this.property = property; 
 } 
} 
 
public class Main 
{ 
 public static void main(String[] args){ 
 Bar bar = new Bar("bar"); 
 method1(bar); // here we are changing the instance that the reference variable "bar" refers 
 method2(bar); // here we are not changing the reference! 
 } 
 public static void method1(Bar m1bar) { 
 mbar.setProperty("modifybar"); 
 
 } 
 public static void method2(Bar m2bar) { 
 Bar newbar = new Bar("newbar"); 
 m2bar = newbar; 
 } 
} 
 
</pre>
<div style="background-color: #f2f9fc; border: 1px solid #c9e6f2; border-radius: 3px; padding: 16px; line-height: 1.45;"><span style="color: red; font-size: x-large; text-align: center;"><b>Popular Tutorials</b></span></p>
<ul style="text-align: left;">
<li><b><a href="https://dineshonjava.com/spring-tutorial/"><em><strong>Spring Tutorial</strong> </em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-web-mvc-framework-chapter-38/"><strong><em>Spring MVC Web Tutorial </em></strong></a></b></li>
<li><b><a href="https://dineshonjava.com/introduction-to-spring-boot-a-spring-boot-complete-guide/"><strong>Spring Boot Tutorial</strong> </a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-take-baby-step-to-secure/"><em>Spring Security Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-tutorial-with-example-aspect-advice-pointcut-joinpoint/"><em>Spring AOP Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/using-spring-jdbc-framework-chapter-32/"><em>Spring JDBC Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-hateoas-hypermedia-driven-restful-web-service/"><em><strong>Spring HATEOAS </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/microservices-with-spring-boot/"><em><strong>Microservices with Spring Boot</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/jax-rs-web-service-tutorial/"><strong><em>REST Webservice</em> </strong></a></b></li>
<li><b><a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/"><em><strong>Core Java </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/hibernate-3-on-baby-steps/"><em><strong>Hibernate Tutorial</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-batch-process-with-example/"><strong><em>Spring Batch</em> </strong></a></b></li>
</ul>
</div>
<p>In above example, method1() logical address bits of first instance(bar) are copied to another reference variable(m1bar), thus resulting both references to point a single memory location where actual object is stored. Remember, making another reference to null will not make first reference also null. But, in changing the property from either reference variable have impact seen in other reference also. But in method2() we are creating new reference &#8220;newbar&#8221; of same class i.e. new memory location of instance and after that change the value of the passing reference variable i.e m2bar assign to new memory location.</p>
<p>Let&#8217;s see step by step in java when we pass any reference of custom types as any method parameters always the memory address is copied to new reference variable. See in below picture:</p>
<p><b>1. Bar bar = new Bar(&#8220;bar&#8221;);</b><br />
This statement will create an instance of class Bar, with &#8220;property&#8221; initialized to &#8220;bar&#8221;. The reference to this created instance is assigned to variable bar;</p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2017/02/bar.png" border="0" /></div>
<p><b>2. define public static void method1(Bar m1bar) </b><br />
In this line of execution then a reference of type Bar with a name m1bar is declared and it’s initially assigned to null.</p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2017/02/bar-1.png" border="0" /></div>
<p><b>3. call of method1(m1bar)</b><br />
This statement assign same memory location of object with both reference variables.</p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2017/02/bar-2.png" border="0" /></div>
<p><b><br />
</b> <b>4. mbar.setProperty(&#8220;modifybar&#8221;);</b><br />
This statement change the value property of instance and it is change for both reference variables.</p>
<div class="separator" style="clear: both; text-align: center;"><b><img src="https://dineshonjava.com/wp-content/uploads/2017/02/bar-3.png" border="0" /></b></div>
<p><b>5. in method2(m2bar) create new reference with other instance with property &#8220;newbar&#8221; as Bar</b></p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2017/02/bar-4.png" border="0" /></div>
<p><b>6. newbar = new Bar(&#8220;newbar&#8221;);</b></p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2017/02/bar-5.png" border="0" /></div>
<p><b><br />
</b> <b>7. m2bar=newbar</b><br />
Here, we have three reference variables bar, m2bar and newbar when statement executes, m2bar and newbar will point to same instance created inside the method. But bar is unchanged and it is continually pointing to instance, it was pointing originally.</p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2017/02/bar-6.png" border="0" /></div>
<p>Hope it help to clear you concept about pass by value in java.</p>
<p>Happy learning!!!.</p>
<p> ;</p>
</div>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/difference-between-save-vs-persist-in-hibernate/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/what-is-marker-interface-in-java/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '86'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…