File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes whats the difference between Objects and reference variables? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "whats the difference between Objects and reference variables?" Watch "whats the difference between Objects and reference variables?" New topic
Author

whats the difference between Objects and reference variables?

Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1050

wats the difference between object and reference variables???


http://www.lifesbizzare.blogspot.com || OCJP:81%
Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
Well, same as the difference between your name and you yourself.


Thanks and Regards
rohan yadav
Ranch Hand

Joined: Oct 13, 2009
Posts: 156
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.


Sage of The Monstrous Toad of Mount Myoboku
Rakesh Shankaranarayana
Greenhorn

Joined: Oct 22, 2008
Posts: 5

Object contains methods and variables, whereas reference variable consists sequence of bits i.e way to access your object.
Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1050

can anybody please specify a simple example of how are objects declared and reference variables declared
Rahul P Kumar
Ranch Hand

Joined: Sep 26, 2009
Posts: 188
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

Joined: Aug 01, 2009
Posts: 1050

ok

wat if we write code like this


A a;// is this referece variable for class a???
Rahul P Kumar
Ranch Hand

Joined: Sep 26, 2009
Posts: 188
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

Joined: Aug 01, 2009
Posts: 1050

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

Joined: Sep 26, 2009
Posts: 188
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: whats the difference between Objects and reference variables?
 
Similar Threads
Calling Overridden methods and overshadowed variables
CLASSPATH vs PATH
Calling Overridden methods and overshadowed variables
what is difference between
Variables in Java