File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes what does it stand for >>>>reference variable.reference variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "what does it stand for >>>>reference variable.reference variable" Watch "what does it stand for >>>>reference variable.reference variable" New topic
Author

what does it stand for >>>>reference variable.reference variable

Deepak Bobal
Ranch Hand

Joined: Feb 06, 2008
Posts: 96
class Mixer
{
Mixer()
{}
Mixer(Mixer m)
{
m1=m;
}
Mixer m1;
public static void main(String[] arg)
{
Mixer m2=new Mixer();
Mixer m3=new Mixer(m2);
m3.go();
Mixer m4=m3.m1;
m4.go();
Mixer m5=m2.m1;
m5.go();
}
void go()
{
System.out.println("Hi");
}
}

This code sample is on page 261 of K n B.
Output is
*********************************
Hi
Hi
java.lang.NullPointerException
**********************************
I am not getting how this output is coming.
And I am unable to understand the meaning of
Mixer m4=m3.m1;
and
Mixer m5=m2.m1;

what does it stand for >>>>reference variable.reference variable

as here m3.m1
and m2.m1

Thanks
Deepak


Constant dripping hollows out a stone....
Ian Edwards
Ranch Hand

Joined: Aug 14, 2006
Posts: 107
A reference variable is a pointer to an object. When you have a statement like



what you are saying is access the object pointed at by the variable m3, get the value of that objects instance variable m1 and assign it to the reference variable m4.

The NullPointerException that is being reported in your program is due to the way that the object m2 is being instantiated. The Mixer class has 2 constructors. The m2 object is being instantiated with the no-args constructor - this means that its m1 instance variable is null.

Later when you attempt to set m5:


it is set to null. Next you try to run the go() method - this fails as m5 is null, hence the exception.
Nadeem Khan
Ranch Hand

Joined: Nov 27, 2007
Posts: 108
Mixer m2=new Mixer(); //m2's Mixer object has m1 as null(default value)
Mixer m3=new Mixer(m2);// m3's Mixer object has m1 as m2!!

Mixer m4=m3.m1;//m4 points to whatever m3's m1 points to.So its same as m2!

Mixer m5=m2.m1;//m5 points to m2's m1 i.e null!
m5.go();//you are trying to call something on null! --> nullpointer //exception!


<i>If there were no Exceptions (not Errors!), Life would have kinda sucked!!</i>
Deepak Bobal
Ranch Hand

Joined: Feb 06, 2008
Posts: 96
Thanks Ian and Mr. Khan ,explanations given by you clear my doubts.
Thanks once again....
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
But when i change method go to static declaration it prints Hi 3 times.
Here is the code :
public class Mixer
{
Mixer()
{}

Mixer(Mixer m)
{
m1 =m;
}

Mixer m1;

public static void main(String a[])
{

Mixer m2=new Mixer();
Mixer m3= new Mixer(m2); // m3.m1=m2//
Mixer m4= m3.m1;
Mixer m5= m2.m1;

m3.go();
m4.go();
m5.go();
}

public static void go()
{ System.out.println("hi");}
}

o/p --
hi
hi
hi

Can now anyone shed some lights on this .


Thanks<br />Dinesh
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
go() is a static method.
You donot require an instance variable to execute the method.


The future belongs to those who believe in the beauty of their dreams.Dream BIG!
Deepak Bobal
Ranch Hand

Joined: Feb 06, 2008
Posts: 96
Hi Dinesh
I also made few changes in program that reflects answer for you

class Mixer
{
Mixer()
{}
Mixer(Mixer m)
{
m1=m;
}
static Mixer m1=null; // line a
public static void main(String[] arg)
{
Mixer m2=new Mixer();
Mixer m3=new Mixer(m2);
m3.go();
Mixer m4=m3.m1;
m4.go();
Mixer m5=m2.m1;
go(); //line b
System.out.println(m1==null); // line c
System.out.println(m4==null); // line d
}
static void go()
{
System.out.println("Hi");
}
}

Notice that i made m=null enev then we are able to call method go() that is static so it conclude that that we don't require class' instance to call static method. line b also clarify this.
Regards
Deepak
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
when i executed your program.
o/p is
Hi
Hi
Hi
false
false

Why it is false, m1 is equal to null then it should print true .
Can you shed some lights on this.
Deepak Bobal
Ranch Hand

Joined: Feb 06, 2008
Posts: 96
class Mixer
{
Mixer()
{}
Mixer(Mixer m)
{
//m=null; //line a
m1=m;
}
static Mixer m1=null;
public static void main(String[] arg)
{
Mixer m2=new Mixer();
System.out.println("before"+(m1==null));

Mixer m3=new Mixer(m2);

System.out.println("After"+(m2==null));
m3.go();
Mixer m4=m3.m1;
m4.go();
Mixer m5=m2.m1;
go();
System.out.println(m1==null);
System.out.println(m4==null);
System.out.println(m3==null);
System.out.println(m2==null);
}
static void go()
{
System.out.println("Hi");
}
}



Try this program You will get your answer yourself.No explanation required.

************************
Next time uncomment line a , and notice the difference.Remember same class objects share same copy of static variable.

****************************
If still have any difficulty ....Post Again.
I'll try to make it more clear.

Regards
Deepak
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what does it stand for >>>>reference variable.reference variable