GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Difference between instance variable and reference variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Difference between instance variable and reference variable" Watch "Difference between instance variable and reference variable" New topic
Author

Difference between instance variable and reference variable

Vallabhaneni Suresh Kumar
Ranch Hand

Joined: Mar 01, 2004
Posts: 68
What is the difference between instance variable and reference variable? I am very much confused between them?
Svend Rost
Ranch Hand

Joined: Oct 23, 2002
Posts: 904
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

Joined: Apr 29, 2005
Posts: 151
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

Joined: May 18, 2005
Posts: 84
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.


I'm a Hood Ornament
Svend Rost
Ranch Hand

Joined: Oct 23, 2002
Posts: 904
@ 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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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."


[Jess in Action][AskingGoodQuestions]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Megs Maquito
Ranch Hand

Joined: May 18, 2005
Posts: 84
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.
 
GeeCON Prague 2014
 
subject: Difference between instance variable and reference variable