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