aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes what really it mean's Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "what really it mean Watch "what really it mean New topic
Author

what really it mean's

sumit kothalikar
Ranch Hand

Joined: Apr 15, 2010
Posts: 91

See the following code it is taken from K&B:




I am confused with what really [u ]m3.m1 [/u] at line 1
and m2.m1 at line 2 means.
What is the meaning of m2 object's m1 at line 2.


Thanks & Regards
Sumit Kothalikar
Rajeev Rnair
Ranch Hand

Joined: Mar 22, 2010
Posts: 308

sumit kothalikar wrote:See the following code it is taken from K&B:




I am confused with what really [u ]m3.m1 [/u] at line 1
and m2.m1 at line 2 means.
What is the meaning of m2 object's m1 at line 2.


For these type of questions, please draw a rough diagram, then the picture will be clear!
The key point here is
class Mixer HAS-A m1 of type Mixer(again)

so we can write


like that!


SCJP6, SCWCD5, OCP-JBCD5, OCE-JWSD6 OCE-JPAD6 , OCM-JEA5 1,OCM-JEA5 2,3 - Brainbench certifications: J2EE, Java2, Java2-NonGUI, JSP, SQL2000 Admin, SQL2000 Programming , Brainbench certified Java Programmer, Computer Programmer, Web Developer, Database Administrator
sumit kothalikar
Ranch Hand

Joined: Apr 15, 2010
Posts: 91

Still not clear
Will you please explain what really happens at line1 and line 2 in in the below program


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);

Mixer m4=m3.m1; //line 1
Mixer m5 = m2.m1; //line2

m3.go();
m4.go();
m5.go();
}
void go(){ System.out.println("hi"); }
}





Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 438

Hello sumit
well what are we doing here?
we are declaring the reference variable m1 of type Mixer
and we are creating object with Mixer m2 = new Mixer();

Now m1 is the instance variable isn't it?
now we can access instance variables of the class using class objects isn't it ?
suppose there is an instance variable int i ;
how do we refer to it if we have object m2?
we refer to it as m2.i
right?
or suppose
we have a instance variable of type Integer
that is
Integer i;
and we can refer to it as m2.i;
in the program
let us go step by step

we are creating the object

also

what happened here? m3 object is created and as we are pasisng m2 to the constructor it is assigned to the reference variable m1 through constructor initialization
and hence m1 is pointing to object of m2() isn't it?
now

m3.m1 will point to the object pointed by m2
and m5.m1 will point to null as m1 is not initialized
Hope this is clear now sumit
Happy coding

SCJP 6 [86%] June 30th, 2010
OCPWCD [84%] March 26th, 2013
If you find any post useful, click the "plus one" sign on the right
Moguluri Ravi Kiran
Ranch Hand

Joined: Apr 16, 2010
Posts: 62

this picture may help us



[Thumbnail for links.jpg]

sumit kothalikar
Ranch Hand

Joined: Apr 15, 2010
Posts: 91

Thanks
Rajiv, Prasad and Moguluri.
I got our point.
Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
Hi,

If I change the line 16 to static void go(){ System.out.println("hi"); } , I am not getting Null Pointer Exception..What is the reason ? I just added "static" keyword.


OCPJP 6.0-81% | Preparing for OCWCD
http://www.certpal.com/blogs/cert-articles | http://sites.google.com/site/mostlyjava/scwcd |
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Harikrishna Gorrepati wrote:Hi,

If I change the line 16 to static void go(){ System.out.println("hi"); } , I am not getting Null Pointer Exception..What is the reason ? I just added "static" keyword.


If you declare the method with static, then no need of instance to invoke that method. If you invoked a static method with an instance, the compiler will automatically, handle it as calling it with the class. Not on the object, so if the object reference is null, no problem.


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: what really it mean's
 
Similar Threads
Q4,Self Test, Chapter 3 (K & B)
K & B question ???
Clarification on Q6 Self-Test Chapter 3 K&B SCJP5
explain output
StackOverflowError