- Java Programming Basics
- Java Tutorial
- Java Environment Setup
- Java Separators
- Java Data Types
- Java Variables
- Java Variable Scope
- Java Type Casting
- Java Operators
- Java Increment Decrement
- Java Left Shift
- Java Right Shift
- Java Bitwise Operators
- Java Ternary Operator
- Java Control Statements
- Java if-else statement
- Java for Loop
- Java while Loop
- Java do-while Loop
- Java switch Statement
- Java break Statement
- Java continue Statement
- Java Popular Topics
- Java Arrays
- Java Multidimensional Array
- Java Strings
- Java Methods
- Java Date and Time
- Java Exception Handling
- Java File Handling
- Java OOP
- Java Classes and Objects
- Java Constructors
- Java Constructor Overloading
- Java Object as Parameter
- Java Returning Objects
- Java Encapsulation
- Java Abstraction
- Java Inheritance
- Java Polymorphism
- Java Packages
- Java Import Statement
- Java Multithreading
- Java Suspend Resume Stop Thread
- Java Programming Examples
- Java Programming Examples
Returning objects in Java with an example
Similar to returning primitive types, Java methods can also return objects. A reference to the object is returned when an object is returned from a method, and the calling method can use that reference to access the object.
You can use the following general form to return an object in the Java programming language:
public class ClassName { // Fields or attributes private int attribute1; private String attribute2; // Constructor public ClassName(int attribute1, String attribute2) { this.attribute1 = attribute1; this.attribute2 = attribute2; } // Method that returns an object of the same class public ClassName methodName() { int newVariable1 = attribute1 + 10; String newVariable2 = attribute2.toUpperCase(); // Return an object of the same class return new ClassName(newVariable1, newVariable2); } }
The class ClassName has a constructor that takes two parameters and two attributes, labeled attribute1 and attribute2. Another method in the class, methodName, returns an object of the same class, ClassName.
In the methodName method, two new variables newVariable1 and newVariable2 are created by performing some operations on the attributes attribute1 and attribute2. Then, a new object of the same class ClassName is created using these new variables as arguments to the constructor, and that new object is returned using the "return" keyword.
Java returning an object example
The following Java program illustrates the concept of returning an object from a method:
public class ObjectAsReturnValueExample { private int attribute1; private String attribute2; public ObjectAsReturnValueExample(int attribute1, String attribute2) { this.attribute1 = attribute1; this.attribute2 = attribute2; } public ObjectAsReturnValueExample modifyObject() { int newVariable1 = attribute1 + 10; String newVariable2 = attribute2.toUpperCase(); return new ObjectAsReturnValueExample(newVariable1, newVariable2); } public static void main(String[] args) { ObjectAsReturnValueExample obj1 = new ObjectAsReturnValueExample(5, "codescracker"); ObjectAsReturnValueExample obj2 = obj1.modifyObject(); System.out.println("Object 1: attribute1 = " + obj1.attribute1 + ", attribute2 = " + obj1.attribute2); System.out.println("Object 2: attribute1 = " + obj2.attribute1 + ", attribute2 = " + obj2.attribute2); } }
Object 1: attribute1 = 5, attribute2 = codescracker Object 2: attribute1 = 15, attribute2 = CODESCRACKER
This program defines a class ObjectAsReturnValueExample with two private attributes attribute1 and attribute2. The constructor of the class initializes these attributes with the passed arguments. The class also includes a modifyObject() method that produces an object that belongs to the same class. A new object is created with the modified values after the attribute1 and attribute2 values of the current object are modified inside the method. The modified values of attribute1 and attribute2 are, respectively, increased by 10 and made uppercase.
Two objects of the ObjectAsReturnValueExample class are created in the main() method. The first object is created with attribute1 and attribute2 initial values of 5 and "codescracker," respectively. The second object is created by calling the first object's modifyObject() method. The modified object is thus returned and assigned to the second object.
Finally, the main() method prints the values of attribute1 and attribute2 for both objects. We can observe that the values of attribute1 and attribute2 of the first object remain unchanged while the second object has modified values. This demonstrates the concept of "object as return value".
« Previous Tutorial Next Tutorial »