Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Deepak Bobal
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ian Edwards
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Deepak Bobal
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ian and Mr. Khan ,explanations given by you clear my doubts.
Thanks once again....
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
go() is a static method.
You donot require an instance variable to execute the method.
 
Deepak Bobal
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic