• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reference variable concepts  RSS feed

 
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can somebody please clarify what these mean with examples..

1. A reference variable can be of only one type and once declared, that type can never be changed(although the object it references can change)

2. A reference is a varaible ,so it can be reassigned to other objects(unless the reference is declared final)

3. A reference variable's type determines the methods that can be invoked on the object the variable is referencing.

4. A reference variable can refer to any object of the same type as the declared reference , or - it can refer to any subtype of the declared type.

5. A reference variable can be declared as a class type or an interface type. If the variable is declared as an interface type, it can reference any object of any class that implements the interface.

Thank you.
 
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like homework to me. What are your answers?
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Home work...... very true

well jose make change in your second statement
A reference variable is not reassigned to other object BUT other object can be reassigned into reference variable.(if there is some super-sub relationship). An object occupy memory in heap while reference denote that memory OR object.
variable mean whose value can vary....... so its true a reference variable can be reassigned into other object off course should not be final.

do study some good book you will get to know more. OR see this link if it helps

Casting



 
jose chiramal
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pankaj Vijay,

The statements mentioned above are from the SCJP book itself. I dont think there can be any changes to them
1. A reference variable can be of only one type and once declared, that type can never be changed(although the object it references can change) [ Reference variable can be of only one type : what does this mean ? ]

2. A reference is a varaible ,so it can be reassigned to other objects(unless the reference is declared final). [ I presume here that a final reference is a reference that cannot be assigned to other objects ??]

Thanks.
 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at the JavaRanch Campfire story Cup Size if you want to learn how variables work, what references are etc.
 
pankaj vijay
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey jose i am not Einsten .. i wrote what i learnt from the books. But yes i tried to write that in a simple language.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
2. A reference is a varaible ,so it can be reassigned to other objects(unless the reference is declared final)


Yes, I also do not understand about this bc I thinks final cannot reassign to others objects. If you can, could you please give me example about it?

Many thanks
 
Huynh Nhung
Greenhorn
Posts: 2
 
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Reference variable is like any other variable type int , float.. Only difference is that it store the reference. So say if you defined any variable of int type then is it possible to change it to other type during life cycle of that program ? No , na .. same is applicable on reference variable as well. Hence we cannot change type of reference variable once defined..

Once we made any variable final we can not re-initialize that variable. It gives compilation error if try . ex:



But if i make e1 final then e2 = e1 statement will give compilation error.
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Huynh Nhung, welcome to the Ranch
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. A reference variable can be of only one type and once declared, that type can never be changed(although the object it references can change)

Example: Means I have a Class Test
Test t1;

Once I declared t1 as type of Test. Its type is never going to change.

2. A reference is a varaible ,so it can be reassigned to other objects(unless the reference is declared final)

Example: Above we have created a reference - Test t1;
t1 can store its own class and subclass object as well, even we can reassign the same reference to different class. Below is the example

Like - Class ChileTest extends Test and another class - Class AnotherTest extends Test

t1 = new ChildTest();
t1 = new AnotherTest();

If we will use final then reassigning is not possible.

3. A reference variable's type determines the methods that can be invoked on the object the variable is referencing.

Example: AnotherTest class has one method doCalculation();
You can call doCalculation() method using super class variable.

4. A reference variable can refer to any object of the same type as the declared reference , or - it can refer to any subtype of the declared type.

Example: Its answer is same as for point no. 2 except final

5. A reference variable can be declared as a class type or an interface type. If the variable is declared as an interface type, it can reference any object of any class that implements the interface.

Example - we have a interface: Interface BaseInterface { }
Class Test implements BaseInterface {}

Then we can create the reference of BaseInterface.
BaseInterface baseInterface = new Test();

Please let me know if you are not clear.


 
Greenhorn
Posts: 29
IntelliJ IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
2. A reference is a variable ,so it can be reassigned to other objects (unless the reference is declared final)

Example:
final Person MY_BOSS = new Person();
Person person = MY_BOSS;
Person otherPerson = MY_BOSS;


 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!