By: aathishankaran in Java Tutorials on 2007年03月06日 [フレーム]
In Java, an object reference variable is a variable that holds the memory address of an object. It does not actually hold the object itself, but instead points to the memory location where the object is stored.
Here's an example:
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
public class Main {
public static void main(String[] args) {
Person person1 = new Person("John", 25);
Person person2 = person1; // assigning person1 to person2
System.out.println(person1.getName()); // prints "John"
System.out.println(person2.getName()); // prints "John"
person2.setName("Jane"); // changing person2's name
System.out.println(person1.getName()); // prints "Jane"
System.out.println(person2.getName()); // prints "Jane"
}
}
In this example, we have a Person class with a name and age, and a Main class that creates two Person objects, person1 and person2. When we assign person1 to person2, we are actually copying the memory address of the object that person1 points to. So both person1 and person2 now point to the same object in memory.
When we change the name of person2, we are actually changing the name of the object that both person1 and person2 point to. This is why when we print out the names of person1 and person2 after the change, they both have the value "Jane".
In summary, object reference variables in Java allow us to point to and manipulate objects in memory without actually holding the object itself.
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
Most Viewed Articles (in Java )
Step by Step guide to setup freetts for Java
Open a .docx file and show content in a TextArea using Java
concurrent.Flow instead of Observable class in Java
DateFormat sample program in Java
Simple Port Scanner application using Java
Using the AWS SDK for Java in Eclipse
Read a file having a list of telnet commands and execute them one by one using Java
Calculator application in Java
Latest Articles (in Java)
Read a file having a list of telnet commands and execute them one by one using Java
Open a .docx file and show content in a TextArea using Java
Step by Step guide to setup freetts for Java
Of Object, equals (), == and hashCode ()
Using the AWS SDK for Java in Eclipse
DateFormat sample program in Java
concurrent.Flow instead of Observable class in Java
Calculator application in Java
Sending Email from Java application (using gmail)
Read a file having a list of telnet commands and execute them one by one using Java
Open a .docx file and show content in a TextArea using Java
Step by Step guide to setup freetts for Java
Of Object, equals (), == and hashCode ()
Using the AWS SDK for Java in Eclipse
DateFormat sample program in Java
concurrent.Flow instead of Observable class in Java
Calculator application in Java
Sending Email from Java application (using gmail)
© 2023 Java-samples.com
Tutorial Archive: Data Science React Native Android AJAX ASP.net C C++ C# Cocoa Cloud Computing EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Trends WebServices XML Office 365 Hibernate
Latest Tutorials on: Data Science React Native Android AJAX ASP.net C Cocoa C++ C# EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Cloud Computing WebServices XML Office 365 Hibernate