• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

whats the difference between Objects and reference variables?

 
Ranch Hand
Posts: 1087
Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
wats the difference between object and reference variables???
 
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, same as the difference between your name and you yourself.
 
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's like refrence variable is used to refer to an object. and object is what you instantiate from a class.
object has memory associated with it and refrence variable is like pointer.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Object contains methods and variables, whereas reference variable consists sequence of bits i.e way to access your object.
 
Vishal Hegde
Ranch Hand
Posts: 1087
Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can anybody please specify a simple example of how are objects declared and reference variables declared
 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vishal Hegde wrote:can anybody please specify a simple example of how are objects declared and reference variables declared


let us take very simple example. when you write Here new A() creates an object of A, puts it on heap. mind that you can create multiple objects of class A and they all will be allocated different address in heap. Whenever you create an object of class, it will have its own copy of class members (this is what Rakesh meant). To make use of this newly create object you want to have some reference to it (optional, but useful). So 'a' is a variable of type A (you can choose to have super type). Now you refer newly created object by 'a', so the name reference variable. there are many more aspects of it like declaring type of variable, declaring variable final, static, concept of runtime polymorphism etc. I felt, you want the basic concept to start up. You need to further study on your own. There is lot of material on internet regarding your queries.
 
Vishal Hegde
Ranch Hand
Posts: 1087
Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok

wat if we write code like this


A a;// is this referece variable for class a???
 
Rahul P Kumar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vishal Hegde wrote:ok

wat if we write code like this


A a;// is this reference variable for class a???


No, that is just reference variable currently referring to null. You see, there is no object (not to mention about object of A) in sight. Ok, probably 'A' is confusing you. that is type of variable 'a'. How do you declare an int variable. you say "int i;". Here, int is type of i, but 'i' is not referring to any value yet, so no value for 'i' (bar default value). It is only when you say int i = 5; (declaration and definition at same line) or after declaring, define it like i =5; then it starts referring to value '5'. Think on same line.
 
Vishal Hegde
Ranch Hand
Posts: 1087
Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh thanks Rahul



i just found a few statements can you give an example for each please...

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)

A reference is a variable , so it can be reassigned to other object(unless the reference is declared final)

A reference variable types determine the methods that can be invoked on the object the variable is referencing

a reference variable can refer to any object of the same type as the declared reference or it can refer to subtype of the declared type

a reference type can be declared class type or an interface type If the variable is declared as an interface type if the variable is declared of reference type it can reference any object of any class that implements the interface
 
Rahul P Kumar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Putting some effort on your side will be best for you. I thought you are finding difficult to start up, so I helped you. All those answers you can get on web.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic