• 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

what really it mean's

 
Ranch Hand
Posts: 91
Notepad
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 310
1
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
 
sumit kothalikar
Ranch Hand
Posts: 91
Notepad
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"); }
}






 
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 63
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this picture may help us
links.jpg
[Thumbnail for links.jpg]
Links
 
sumit kothalikar
Ranch Hand
Posts: 91
Notepad
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks
Rajiv, Prasad and Moguluri.
I got our point.
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
rubbery bacon. crispy tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic