Forums Register Login

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

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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!
+Pie Number of slices to send: Send
Thanks Ian and Mr. Khan ,explanations given by you clear my doubts.
Thanks once again....
+Pie Number of slices to send: Send
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 .
+Pie Number of slices to send: Send
go() is a static method.
You donot require an instance variable to execute the method.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
Can you smell this for me? I think this tiny ad smells like blueberry pie!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1159 times.
Similar Threads
Question
what really it mean's
assignment
explain output
what is the answer
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 08:30:23.