Assigning Object Reference Variables Assignment Help

Assignment Help: >> Classes and Objects in Java - Assigning Object Reference Variables

Assigning object reference variables

Object reference variables act differently than you may expect whenever an assignment took place. For instance, what do you think the subsequent fragment does?

Box b1 = new Box ();

Box b2 = b1;

You may think in which b2 is being assigned a reference to a copy of the object referred to by b1. That is you may think in which b1 and b2 refer to divided and distinct objects. Moreover, this would be wrong. Alternatively, after this fragment executes, b1 and b2 will both refer to the similar object. The assignment of b1 to b2 did not allocate any memory or copy any category of the original object. It easily makes b2 refer to the similar object as does b1. Therefore any modifies made to the object by b2 will affect the object to which b1 is referring, since they are the similar object.

This situation is depicted here:

992_object reference variable.png

Although b1 and b2 both refer to the similar object, they are not connected in any other way. For instance, a subsequent assignment to b1 will easily unhook b1 from the original object without affecting the object or affecting b2 for instance

Box b1 = new Box ();

Box b2 = b1;

// ...

b1=null;

In the given example, b1 has been set to null, but b2 yet points to the original object.

Introducing methods

Classes commonly consist of two things: examples methods and variables. The topic of methods is a huge one since Java gives them so much power and flexibility. Actually, much of the next chapter is developed to methods. Although, there are a few fundamentals which you want to learn now so which you could starts to add methods to your classes.

This is the common form of a method

Type name(parameter-list){

//body of method

}

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd