Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

whats the difference between Objects and reference variables?

 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wats the difference between object and reference variables???
 
Satya Maheshwari
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, same as the difference between your name and you yourself.
 
rohan yadav
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • 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.
 
Rakesh Shankaranarayana
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can anybody please specify a simple example of how are objects declared and reference variables declared
 
Rahul P Kumar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • 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: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic