Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Reference variable concepts

 
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67636
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sounds like homework to me. What are your answers?
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Refer this: https://coderanch.com/t/562993/java/java/reference-variable
 
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 75644
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Huynh Nhung, welcome to the Ranch
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;


 
Just the other day, I was thinking ... about this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic