aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt on object 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 "doubt on object" Watch "doubt on object" New topic
Author

doubt on object

ansuman mohapatra
Greenhorn

Joined: Mar 04, 2008
Posts: 27

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.


Cleared SCJP.....
Mihai Fonoage
Ranch Hand

Joined: Feb 25, 2006
Posts: 39
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
Ramesh Sahu
Ranch Hand

Joined: Jul 14, 2008
Posts: 32
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.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14435
    
  23

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?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
ansuman mohapatra
Greenhorn

Joined: Mar 04, 2008
Posts: 27
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)
vaibhav mishra
Ranch Hand

Joined: Jun 18, 2008
Posts: 168
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>


SCJP
ansuman mohapatra
Greenhorn

Joined: Mar 04, 2008
Posts: 27
thanks a lot..i got it..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubt on object