File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between instance variable and reference variable

 
Vallabhaneni Suresh Kumar
Ranch Hand
Posts: 68
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the difference between instance variable and reference variable? I am very much confused between them?
 
Svend Rost
Ranch Hand
Posts: 904
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An instance variable is a variable that belongs to the instance of an
object (opposed to class variables). A reference varaible, points to
an object. A reference variable can however also be an instance member.

E.g.


So, in the above code you can see the instance members - that is, the
players legs and his name. His specie isn't an instance member though,
since all objects of type SoccerPlayer has the same specie.

left and right points to the same object, and if for instance left changes
it's shooting skill, right's shooting skill will also change

/Svend Rost
 
ganesh pol
Ranch Hand
Posts: 151
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi my dear friends
your question is
What is the difference between instance variable and reference variable? I am very much confused between them?

first we will see what is instance variables
1] instance variable may be primitive(eg double float etc) and non primitive (array ,string) ( non primitive variables are varibles of which we can create object)

1]instance variables are member of class i.e whenever we create object of class in that case object which has same variables that is assigned in class are created more technically these are known as states

this state of object is so much important consider for example we have created one class person which has instance variable age

public class person
{//class variables i.e variables marked static
//instance vaiable declaration i. variables which are not static
// and define out side method<<<<--- ans to instance varibles
private double age;<<<-- eg of primitive variables
private String name;<<<<----eg of non primitive instance variable

}


now what is mean by reference variable
u might see how do we create objects in jave
Class_name ref_var= new class_name();

eg String st= new String("ganesh new comer to java");
here st is reference variable pointing to string object
 
Megs Maquito
Ranch Hand
Posts: 84
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is it correct to say the ff:

1. when you initialize a variable, that is an instance variable. e.g.

2. when you initialize an object, that is a reference variable. e.g.
 
Svend Rost
Ranch Hand
Posts: 904
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Megs:

Well.. they can both be instance variables, but the primitives
(int, double, ect.) can't be a reference variable as you call it, because
they don't "point" to an object saved in memory.

An instance member is a member that belongs to a specific object, whereas a
class member is shared by all the objects.

A variable that get's initialised in a method block isn't an instance member,
e.g.


In this example bar isn't an instance member, flaf is however.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The most direct answer to ganesh pol's question, I think, is that "reference variable" is the correct name for what he calls "non-primitive variable."
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
The most direct answer to ganesh pol's question, I think, is that "reference variable" is the correct name for what he calls "non-primitive variable."


Similarly, an "instance variable" would be a "non-static non-local variable", wouldn't it?

The two terms really are orthogonal.
 
Megs Maquito
Ranch Hand
Posts: 84
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well.. they can both be instance variables, but the primitives
(int, double, ect.) can't be a reference variable as you call it, because
they don't "point" to an object saved in memory.

An instance member is a member that belongs to a specific object, whereas a
class member is shared by all the objects.


Thanks much Svend, that makes it a lot clearer.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic