Call by Reference in Java W3Schools Tutorial Guide

Robert Jul 13, 2026

If you're a Java programmer, you're likely familiar with passing arguments to methods. But do you understand how those arguments are passed? Java uses call by value, where the actual value of the argument is copied and passed to the method. However, when you're dealing with objects, you might sometimes want to pass a reference to the object itself. Let's explore how to do this in Java, using w3schools as our guide.

Call by Value vs Call by Reference
Call by Value vs Call by Reference

W3Schools, a well-known online learning resource, offers an excellent overview of Java's call by value concept. But to delve deeper into call by reference, we first need to grasp the basics of Java's pass-by-value mechanism.

☕ Java Basics Cheat Sheet for Beginners | Learn Java in One Page 🚀
☕ Java Basics Cheat Sheet for Beginners | Learn Java in One Page 🚀

Understanding Call by Value in Java

As mentioned earlier, Java uses call by value. When a method is called, the value of the actual parameter is copied to the formal parameter. So, any changes made inside the method don't affect the original value. Let's take a look at an example from w3schools:

How to Call Method with Parameters in Java - Scientech Easy
How to Call Method with Parameters in Java - Scientech Easy

public class Test { public static void change(int num) { num += 10; } public static void main(String[] args) { int x = 50; System.out.println("Before: " + x); change(x); System.out.println("After: " + x); }}

If we run this code, we'll see that the value of x remains unchanged after the call to the change method. This demonstrates Java's pass-by-value behavior.

Interface in Java
Interface in Java

Why Java Uses Call by Value

You might wonder why Java uses call by value instead of call by reference. The main reason is to prevent unexpected side effects. As James Mc brace, w3schools founder, puts it, "Java uses pass by value to allow a method to manipulate the value of a passed item of data, without affecting the original value."

However, there are situations where you might want to modify the original value, which leads us to our next topic: call by reference.

Top Features of Java Every Beginner Should Know ☕🚀
Top Features of Java Every Beginner Should Know ☕🚀

Call by Reference in Java

To achieve call by reference in Java, you need to work with object references. Instead of passing the value, you pass a reference to the object's location in memory. This way, changes made inside the method will affect the original object. Let's take a look at an example:

public class Test { public static void change(Integer[] arr) { arr[0] = 100; } public static void main(String[] args) { Integer[] x = {50}; System.out.println("Before: " + Arrays.toString(x)); change(x); System.out.println("After: " + Arrays.toString(x)); }}

Learn Java in 30 Days Only
Learn Java in 30 Days Only

In this case, the change method modifies the original array, demonstrating call by reference behavior in Java.

Call by Reference with Object Parameters

Java Class and Objects - Easy Learning with Real-life Examples! - TechVidvan
Java Class and Objects - Easy Learning with Real-life Examples! - TechVidvan
Java Scanner Class Made Easy: Your Ultimate Input Cheat Sheet 🎯💻
Java Scanner Class Made Easy: Your Ultimate Input Cheat Sheet 🎯💻
30 Days of Java – Day 15 | Java Polymorphism Explained for Beginners
30 Days of Java – Day 15 | Java Polymorphism Explained for Beginners
Java Method - Declaring and Calling Method with Example - DataFlair
Java Method - Declaring and Calling Method with Example - DataFlair
Java OOP Concepts Explained for Beginners | Crack Interviews Easily
Java OOP Concepts Explained for Beginners | Crack Interviews Easily
Class and Object in Java Explained | OOP Concepts for Beginners
Class and Object in Java Explained | OOP Concepts for Beginners
Learn Java in 45 Days
Learn Java in 45 Days
Top 5 Java Projects for Beginners
Top 5 Java Projects for Beginners
30 Days of Java – Day 14 | Java Inheritance Explained for Beginners
30 Days of Java – Day 14 | Java Inheritance Explained for Beginners
Java Programming CheatsheetIntro to Java Programming: An Interdisciplinary Approach and Computer Science: An Interdisciplinary Approach by Sedgewick and Wayne
Java Programming CheatsheetIntro to Java Programming: An Interdisciplinary Approach and Computer Science: An Interdisciplinary Approach by Sedgewick and Wayne
Java Patterns Notes 1
Java Patterns Notes 1
How to Learn Java In 50 Days 🔥🚀
How to Learn Java In 50 Days 🔥🚀
While Loop in Java Explained | Java Programming Basics for Beginners
While Loop in Java Explained | Java Programming Basics for Beginners
🔄 MASTER JAVA RECURSION! 🔄
🔄 MASTER JAVA RECURSION! 🔄
Logical Operators in Java: Think Smarter, Code Better ☕🚀
Logical Operators in Java: Think Smarter, Code Better ☕🚀
Java Cheat Sheet: Operators, Scanner & User Input ☕
Java Cheat Sheet: Operators, Scanner & User Input ☕
a poster with different types of writing and numbers on it, including the words'jwa beginner notes '
a poster with different types of writing and numbers on it, including the words'jwa beginner notes '
Java Cheatsheet
Java Cheatsheet
Keywords in Java - Java Reserved Words - TechVidvan
Keywords in Java - Java Reserved Words - TechVidvan

You can also achieve call by reference by passing an object as a parameter. Here's an example from w3schools:

public class Test { public static void changePerson(Person person) { person.name = "John"; } public static void main(String[] args) { Person p1 = new Person("Mary"); System.out.println("Before: " + p1.name); changePerson(p1); System.out.println("After: " + p1.name); }}

In this example, the changePerson method changes the name of the person object passed as a parameter. The original object's property is modified, not just a copy of it. This demonstrates call by reference with object parameters.

Understanding call by reference in Java is crucial for writing efficient and maintainable code. By mastering this concept, you'll be better equipped to handle complex data manipulations. Now that you have a solid grasp of call by reference in Java, go ahead and explore w3schools for more in-depth learning and hands-on examples!