• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Junilu Lacar
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst
  • Peter Rooke
  • Mikalai Zaikin
Bartenders:
  • Himai Minh

Passing Variables into Methods concept

 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone

The code is from K&B chp 3 Quiz _ 4

class Mixer
{

Mixer() ---------------------------------> constructor with no-args
{

}

Mixer(Mixer m)--------------------------> constructor with ref var m
{
m1 = m; -------------------------> ref var m1 = m
}

Mixer m1; ----------------------------> class Mixer has an ref var m1 with no reference to an object (in other word no object is created)

public static void main( String args[])
{


Mixer m2 = new Mixer(); ----------------> fine

Mixer m3 = new Mixer(m2); ---------------------> ref var m2 is passed to Mixer class constructor Mixer(Mixer m) where we have m1 = m2

m3.go(); -----------------------------> Hi is printed

Mixer m4 = m3.m1; ------------------------> m4 ref var is equal to (ref var m3. ref var m1)-----------------> ?

m4.go(); --------------------------> Hi is printed

Mixer m5 = m2.m1; [b]--------------------------> m5 ref var is equal to (ref var m2. ref var m1)-----------------> ?


m5.go() -----------------> why Hi is not printed >


}

void go()
{
System.out.println("hi ");
}
}
 
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
because m5 is null....this will give you null pointer exception
 
dimple bav
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Raju Champaklal wrote:because m5 is null....this will give you null pointer exception



If i m not wrong it goes this way ----

Mixer m5 = m2.m1

Since Mixer m2 = new Mixer() has no arg to passed so m5 is null and hi will be not printed .

Right ?

Thank you .
Dimple

 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
m5 is equal to m2.m1....and m2.m1 is null...do you see anywhere m2.m1 getting initalised aanywhere?
 
dimple bav
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a doubt on this

Mixer m2 = new Mixer(); --------------- > inside void main

Mixer m1; -------------> ouside main

Mixer m5 = m2.m1 -------------------------------> ref var m2. ref var m1

m2 ref var has an object whereas ref var m1 has no object.

So third ref var m5 = m2.m1 ----------------- what doe it mean ?

Thank you .
Dimple
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what do you think how is m1 related to m2?
 
dimple bav
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So

Mixer m4 = m3.m1 -----------------> where intialized ?

Just because m3 ref var is intialized by passing ref var m2 i.e Mixer m3 = new Mixer(m2) where m1 = m2 in Mixer(Mixer m) constructor

Does it mean Mixer m4 = m3.m2 where m3 and m2 are both intialized.

Let me know my concept is right ?

In other words

Mixer m5 = m2.m1 ---------------- where m1 is not intialized .


Thank you.
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what do you think is the difference between these two codes?

Mixer m2 = new Mixer();

Mixer m3 = new Mixer(m2);
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
did you understand why m2.m1 is null?
 
dimple bav
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
m2 ref var has a constructor with no args

and


m3 ref var has a constructor with a ref var m2 passed into it .


Thank you.
 
dimple bav
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
m2.m1 is null

Since m1 is a thus a ref var created with no object.

in other words

Mixer m1 -----------> declare

m1 = new Mixer() ----------> intialized

So m1 is not intialized.


Thank you.
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
m2 ref var has a constructor with no args
what is the difference between calling a no arg constructor and arg constrcuotr? in which case is the instance variable m1 getting initialised?
 
dimple bav
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
m1 will be intialized with arg constructor .

Thank you.
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mixer m2=new Mixer(); in this case m1 is null since m1 gets its default value which is null....nothing is passed to the constructor of m2...

Mixer m3=new new Mixer(m2) means m1 of m3(the instance variable of m3) now refers to the object referring to m2.......

 
reply
    Bookmark Topic Watch Topic
  • New Topic