• 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

From Applied Exam

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is the result of compiling and executing?
Public class StaticTest2{
1: public static void main(String[] args){
2: me().run();
3: }
4: public static StaticTest2 me(){
5: System.out.print("Me");
6: return null;
7: }
8: public static void run(){
9: System.out.println("Run");
10: }
}
The answer is"Me Run will be printed to the system console".
What I don't understand is line 4, the method declaration,
"classname methodname"---is it legal in any sense? And, if me()
method is legal, then we could call run() method in this way?
me().run()?
Thanks a lot.
Indy
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
4: public static StaticTest2 me(){
Is it not the return type?
May be JVM interprets this as the method me()
will return an instance of StaticTest2 class.
What do u think ...
Regds.
- satya
 
Rancher
Posts: 241
Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Indy,
StaticTest2 is the return type. classname methodname is what you will have with any method that returns an object, I would think. if it returned a primitive, you would have primitivename methodname. I hope this helps.
I thought this was a very tricky question, although it was the null reference that threw me. I posted on it just recently if you can find it, it was called "use of null" or something.
Eric
 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Satya,
Your interpratation is right.
Indy,
public static StaticTest2 me(){
// ..
}

is a valid method declaration/defn. Generally the format of a method is
accesslevel (public/protected/private/default)
other modifiers static/final/native/abstract/synchronized..etc)
the_return_type ( anyofprimitivesORRefeType(byte/char/short/int/long/float/double ) OR className/interfaceName/array (of primitive/reftype) OR void )
methodName ( parameters.. )
throws Throwable1,Throwable2, .... THrowable(n)
So the above method is public and static and returns an instance of Class StaticTest2 and its method name is me().
The interesting part here is
me().run() is a valid code. Since me() returns an instance of class StaticTest2, and we can very well call a method on an object of a class, it is valid. Since all static method calls are bound at compile time itself (since static belongs to the class as a whole and not on any particular instance of the class, the compiler binds them at compile time itself) , here what me().run() does is me() is bound first (since it is static), then , since it returns an instance of StaticTest2, calling run() on that, is also statically bound, since run() is also static. note that static methods can be called as ClassName.staticMethod() as well as anInstanceoftheClass.staticMethod(). Here the compiler treats this case as the anInstanceoftheClass.staticMethod() type.
So both me() and run() are statically bound at compile time itself and so it prints me and run. Another point to note here is a static method can be called on a null object instance, wheras you CAN NOT call an instance method. It will throw a NullPointerException at run time. For example the foll sample program you can call the static method m1() on the null ref t1 but not m2().
<pre>
class Test {
public static void main(String[] args) {
Test t1=null;
//t1.m1(); //valid
t1.m2();
}
static void m1() {
System.out.println("hello");
}
void m2() {
System.out.println("hello");
}
}

</pre>
regds
maha anna
[This message has been edited by maha anna (edited May 25, 2000).]
 
Indy
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much! satya, eric, maha. This indeed kicks out some blanks of my java knowledge.
Indy
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic