Forums Register Login

doubt on object

+Pie Number of slices to send: Send
 


class Mixer {
Mixer() { }
Mixer(Mixer m) { m1 = m; }
Mixer m1;
public static void main(String[] args) {
Mixer m2 = new Mixer();
Mixer m3 = new Mixer(m2);
m3.go();
Mixer m4 = m3.m1; \\ line 1
m4.go();
Mixer m5 = m2.m1; \\ line 2
m5.go();
}
void go() { System.out.print("hi "); }
}




What is the result?
A. hi
B. hi hi
C. hi hi hi
D. Compilation fails
E. hi, followed by an exception
F. hi hi, followed by an exception

The answer given as F.

But can any1 explain what is happening at line 1 and line2. I mean what is the result if two objects are joined by a dot operator ?

and I am sorry but I got this question from a online book and I donno the name of this book.
+Pie Number of slices to send: Send
I believe the code is from K&B' book or Master Exam. As for your question, is the instance variable m1 of m2 ever (non-default) initialized? Look at the constructor being called when a new Mixer object is created and assigned to the m2 reference variable.

Mihai Fonoage
+Pie Number of slices to send: Send
Just like a class can have have instance of a particular type.
Mixer class consists of an insatnce of type Mixer.
So m3.m1 is nothing but instance variable m1 of an Mixer object m3.
+Pie Number of slices to send: Send
 

Originally posted by ansuman mohapatra:
and I am sorry but I got this question from a online book and I donno the name of this book.


You don't know the name of the book? Don't you have the book anymore? Can you give us the link to where you found that book?
+Pie Number of slices to send: Send
 

Originally posted by Jesper Young:

You don't know the name of the book? Don't you have the book anymore? Can you give us the link to where you found that book?



Yea i got it ...its from the SCJP Sun Certified Programmer for Java 5 Study Guide(Exam 310-055)
+Pie Number of slices to send: Send
The question is definitely from K&B
I checked it, it is there in it and book definitely explains it quite clearly

let us examine line by line

<blockquote>code:
<pre name="code" class="core">class Mixer {</pre>
</blockquote>
class declaration
<blockquote>code:
<pre name="code" class="core">Mixer() { }
Mixer(Mixer m) { m1 = m; }
</pre>
</blockquote>
overloaded constructores for class declarations
one is simple (like default constructor)
adn other is overloaded which gets an object reference of type Mixer and create a reference variable m1 refer to it
<blockquote>code:
<pre name="code" class="core">Mixer m1;</pre>
</blockquote>
the object reference declared which simply by default refers to nothing if object created with no reference passed
and if a object reference is passed then it refers to it
<blockquote>code:
<pre name="code" class="core">public static void main(String[] args) {</pre>
</blockquote>
start of main method
<blockquote>code:
<pre name="code" class="core">Mixer m2 = new Mixer();</pre>
</blockquote>
object reference m2 created refers to an object of type mixer that have a refence m1 as its instance variable which refers to nothing
<blockquote>code:
<pre name="code" class="core">Mixer m3 = new Mixer(m2);</pre>
</blockquote>
object reference m3 created refers to an object of type mixer that have a refence m1 as its instance variable which refers to object passed to constructor
<blockquote>code:
<pre name="code" class="core">m3.go();</pre>
</blockquote>
first hi is printed
<blockquote>code:
<pre name="code" class="core">Mixer m4 = m3.m1; \\ line 1</pre>
</blockquote>
an object reference m4 is created which refers to m1 same location referred by instance variable m1 of mixer object m3
<blockquote>code:
<pre name="code" class="core">m4.go();</pre>
</blockquote>
another his is printed , nothing is violated
<blockquote>code:
<pre name="code" class="core">Mixer m5 = m2.m1; \\ line 2</pre>
</blockquote>
m5 tends to refer to m1 instance variable (which is an object reference of type mixer but note that the object m2 is created with no parameter passed to constructor and hence its instance refence variable does not refer to anything(it's just declared ,not created yet by new keyword or some other mechanism) so it will throw an exception
<blockquote>code:
<pre name="code" class="core">m5.go();
}
void go() { System.out.print("hi "); }
</pre>
</blockquote>
the go method
<blockquote>code:
<pre name="code" class="core">
}
</pre>
</blockquote>
+Pie Number of slices to send: Send
thanks a lot..i got it..
Getting married means "We're in love, so let's tell the police!" - and invite this tiny ad to the wedding:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1291 times.
Similar Threads
what really it mean's
initialization
explain output
output not clear
what is the answer
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 03:34:25.