Java create reference to object

Class Reference

Reference provides a way of recording address information about objects which themselves are not directly bound to the naming/directory system.

A Reference consists of an ordered list of addresses and class information about the object being referenced. Each address in the list identifies a communications endpoint for the same conceptual object. The «communications endpoint» is information that indicates how to contact the object. It could be, for example, a network address, a location in memory on the local machine, another process on the same machine, etc. The order of the addresses in the list may be of significance to object factories that interpret the reference.

Multiple addresses may arise for various reasons, such as replication or the object offering interfaces over more than one communication mechanism. The addresses are indexed starting with zero.

A Reference also contains information to assist in creating an instance of the object to which this Reference refers. It contains the class name of that object, and the class name and location of the factory to be used to create the object. The class factory location is a space-separated list of URLs representing the class path used to load the factory. When the factory class (or any class or resource upon which it depends) needs to be loaded, each URL is used (in order) to attempt to load the class.

A Reference instance is not synchronized against concurrent access by multiple threads. Threads that need to access a single Reference concurrently should synchronize amongst themselves and provide the necessary locking.

Читайте также:  Java по быстрому практический

Источник

Reference Variable in Java

Before We Started with the Reference variable we should know about the following facts.

1. When we create an object (instance) of class then space is reserved in heap memory. Let’s understand with the help of an example.

Reference Variable

Now, The space in the heap Memory is created but the question is how to access that space?.

Then, We create a Pointing element or simply called Reference variable which simply points out the Object(the created space in a Heap Memory).

Heap Memory

Understanding Reference variable

1. Reference variable is used to point object/values.

2. Classes, interfaces, arrays, enumerations, and, annotations are reference types in Java. Reference variables hold the objects/values of reference types in Java.

3. Reference variable can also store null value. By default, if no object is passed to a reference variable then it will store a null value.

4. You can access object members using a reference variable using dot syntax.

Java

Let us see what is actually happening step by step.

1. When we create an object of demo class new DEMO();, the default constructor is called and returns a reference of the object, and simply this reference will be stored to the reference variable D1 (As we know that associativity is Right-hand side to left-hand side).

2. The value of a reference variable is a reference. When we attempt to print the value of a reference variable, the output contains the name of the class which has been instantiated concatenated by @ and the hash code created for it by Java: the string Demo@214c265e tells us that the given variable is of type Name and its hexadecimal format of hash code is 214c265e.

3. At this point we will access the methods display() of the class demo using our custom reference variable that we created.

BINDING UP : The constructor call returns a value that is a reference to the newly-created object. The equality sign tells the program that the value of the right-hand side expression is to be copied as the value of the variable on the left-hand side. The reference to the newly-created object, returned by the constructor call, is copied as the value of the variable.

Java

Reference accessing varibale

Java

Methods of an instance

Java

Pointing to same memory

Note:

Here we pass G1 and Q1 reference variable point out the same object respectively. Secondly At Point 1 we try to get the value of the object with G1 reference variable which shows it as 25 and At Point 2 we try to get the value of an object with D1 reference variable which shows it as 25 as well. This will prove that the modification in the object can be done by using any reference variable but the condition is it should hold the same reference.

More on Reference Variable

1. Reference Variable as Method Parameters:

As the value of a primitive variable is directly stored in the variable, whereas the value of a reference variable holds a reference to an object. We also mentioned that assigning a value with the equality sign copies the value (possibly of some variable) on the right-hand side and stores it as the value of the left-hand-side variable. A similar kind of copying occurs during a method call. Regardless of whether the variable is primitive or reference type, a copy of the value is passed to the method’s argument and copied to that argument.

Note: Java only supports pass by value.

But we know that the reference variable holds the reference of an instance(OBJECT) so a copy of the reference is passed to the method’s argument.

Источник

Object References in Java

This topic explains the concept of an object references; it is targeted at people who are new to programming in Java. You should already be familiar with some terms and meanings: class definition, main method, object instance, and the calling of methods “on” an object, and passing parameters to methods.

public class Person < private String name; public void setName(String name) < this.name = name; >public String getName() < return name; >public static void main(String [] arguments) < Person person = new Person(); person.setName("Bob"); int i = 5; setPersonName(person, i); System.out.println(person.getName() + " " + i); >private static void setPersonName(Person person, int num) < person.setName("Linda"); num = 99; >>

To be fully competent in Java programming, you should be able to explain this example to someone else off the top of your head. Its concepts are fundamental to understanding how Java works.

As you can see, we have a main that instantiates an object to the variable person, and calls a method to set the name field in that object to “Bob“. Then it calls another method, and passes person as one of two parameters; the other parameter is an integer variable, set to 5.

The method called sets the name value on the passed object to “Linda’, and sets the integer variable passed to 99, then returns.

So what would get printed?

So why does the change made to person take effect in main, but the change made to the integer does not?

When the call is made, the main method passes an object reference for person to the setPersonName method; any change that setAnotherName makes to that object is part of that object, and so those changes are still part of that object when the method returns.

Another way of saying the same thing: person points to an object (stored on the heap, if you’re interested). Any change the method makes to that object are made “on that object”, and are not affected by whether the method
making the change is still active or has returned. When the method returns, any changes made to the object are still stored on that object.

Contrast this with the integer that is passed. Since this is a primitive int (and not an Integer object instance), it is passed “by value”, meaning its value is provided to the method, not a pointer to the original integer passed in. The
method can change it for the method’s own purposes, but that does not affect the variable used when the method call is made.

In Java, all primitives are passed by value. Objects are passed by reference, which means that a pointer to the object is passed as the parameter to any methods that take them.

One less-obvious thing this means: it is not possible for a called method to create a new object and return it as one of the parameters. The only way for a method to return an object that is created, directly or indirectly, by the
method call, is as a return value from the method. Let’s first see how that would not work, and then how it would work.

Let’s add another method to our little example here:

private static void getAnotherObjectNot(Person person)

And, back in the main, below the call to setAnotherName, let’s put a call to this method and another println call:

getAnotherObjectNot(person);
System.out.println(person.getName());

Now the program would print out:

What happened to the object that had George? Well, the parameter that was passed in was a pointer to Linda; when the getAnotherObjectNot method created a new object, it replaced the reference to the Linda object with a
reference to the George object. The Linda object still exists (on the heap), the main method can still access it, but the getAnotherObjectNot method wouldn’t be able to do anything with it after that, because it has no reference to it. It would appear that the writer of the code intended for the method to create a new object and pass it back, but if so, it didn’t work.

If that is what the writer wanted to do, he would need to return the newly created object from the method, something like this:

private static Person getAnotherObject()

Person mary;
mary = getAnotherObject();
System.out.println(mary.getName());

And the entire program output would now be:

Here is the entire program, with both additions:

public class Person < private String name; public void setName(String name) < this.name = name; >public String getName() < return name; >public static void main(String [] arguments) < Person person = new Person(); person.setName("Bob"); int i = 5; setPersonName(person, i); System.out.println(person.getName() + " " + i); getAnotherObjectNot(person); System.out.println(person.getName()); Person person; person = getAnotherObject(); System.out.println(person.getName()); >private static void setPersonName(Person person, int num) < person.setName("Linda"); num = 99; >private static void getAnotherObjectNot(Person person) < person = new Person(); person.setMyName("George"); >private static person getAnotherObject() < Person person = new Person(); person.setMyName("Mary"); return person; >>

Источник

Оцените статью