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

Need help

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi: Can someone give some detail explanations about the following code, which is compiled and run fine. the outputs are ME followed by RUN. I also confuse the purpose on "return null" in the code. If I change to another or move it etc, and get a err.
Thanks.

public class N {

public static void main(String [] arg){
me().run();
}
public static N me(){
System.out.println("ME");
return null;
}
public static void run(){
System.out.println("RUN");
}
}//ME, RUN
 
Ranch Hand
Posts: 477
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I read the following (I don�t remember where):
When you have a reference to an object that has a null value and you call a static method or make a reference to a static member of that class, there �s no NullPointerException thrown.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I also get confuse. What is the meaning about the line in comment//, N is a object?
Thank you.

public class N {
public static void main(String [] arg){

me().run();
}
public static N me(){///
System.out.println("ME");
return null;
}
public static void run(){
System.out.println("RUN");
}
}//ME, RUN
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually Marcela is right. Because me is declared to return a static reference, call to run is resolved at compile time. That means,
me().run()
gets translated ( at compile time ) to
me();
N.run();
Note that the actual return type of me(), which is null, will not affect the subsequent call to run!!.
Hope this helps.
Ajith
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the byte-code dump of the class file. Note the lines in bold face which substantiate the argument the call to run() is resolved using a static reference.
Method void main(java.lang.String[])
0 invokestatic #8 <Method N me()> --> same as N.me();
3 pop
4 invokestatic #11 <Method void run()> --> same as N.run()
7 return

Method N me()
0 getstatic #9 <Field java.io.PrintStream out>
3 ldc #1 <String "ME">
5 invokevirtual #10 <Method void println(java.lang.String)>
8 aconst_null
9 areturn

Method void run()
0 getstatic #9 <Field java.io.PrintStream out>
3 ldc #2 <String "RUN">
5 invokevirtual #10 <Method void println(java.lang.String)>
8 return
The proof is in the pudding
Ajith
[This message has been edited by Ajith Kallambella (edited August 15, 2000).]
 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Learner:
Hi: Can someone give some detail explanations about the following code, which is compiled and run fine. the outputs are ME followed by RUN. I also confuse the purpose on "return null" in the code. If I change to another or move it etc, and get a err.
Thanks.
public class N {

public static void main(String [] arg){
me().run();
}
public static N me(){
System.out.println("ME");
return null;
}
public static void run(){
System.out.println("RUN");
}
}//ME, RUN



Static Methods don't need an instance of the class to get invoked. So, you can change your code in main() as
me();
run();
It will still produce the same output. Null is a valid Object/Class reference.
HTH,
Savithri
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. all of you. Now I am clear.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic