• 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

Confusing output

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Test {
static String mountain = "Everest";

static Test favorite(){
System.out.print("Mount ");
return null;
}
public static void main(String[] args) {
System.out.println(favorite().mountain);
}
}

Ouput of above program is Mount Everest I am confused is why this is not throwing NullPointerException Any ideas?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because the member variable 'mountain' is static, so no instance of class Test is necessary to access the variable. Java only looks at the return type of the method favorite() which is Test, and then it knows that it has to look in class Test for the static member variable.

If you don't know what 'static' means, read this:
The Java Tutorial - Understanding Instance and Class Members
[ December 30, 2007: Message edited by: Jesper Young ]
 
Bartender
Posts: 2292
3
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy, partner.

Well, it shouldn't throw a NullPointerException

The favorite() method is being called from a static context (public static void main(String [] args)), and it is indeed static. No errors here. This method prints "Mount " and returns a reference to a Test object. This object has a static variable called mountain. It would be almost the same as calling System.out.println(Test.mountain); but the difference is that the favorite() method prints "Mount " and returns a reference to a just created Test object, that's why you can use the static variable mountain.
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Roberto Perillo:
... but the difference is that the favorite() method prints "Mount " and returns a reference to a just created Test object, that's why you can use the static variable mountain.


No, it doesn't; the favorite() method returns null, not a reference to a Test object. Nowhere in the code a Test object is created.

The point is that you don't need an instance of the class to access static members, because static members belong to the class, not to a specific instance of the class.
[ December 31, 2007: Message edited by: Jesper Young ]
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI. Try this code . It throws nullpointer exception.

public class Test {

String mountain = "Everest";

static Test favorite(){
System.out.print("Mountain");

return null;
}
public static void main(String[] args) {
Test t = new Test();
t=t.favorite();
System.out.println();
System.out.println(t.mountain);
}
}
 
Roberto Perillo
Bartender
Posts: 2292
3
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jesper Young:
No, it doesn't; the favorite() method returns null, not a reference to a Test object.



I had just looked at the static Test favorite() signature, silly mistake, sorry guys. It indeed returns null.
 
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Because the member variable 'mountain' is static, so no instance of class Test is necessary to access the variable. Java only looks at the return type of the method favorite() which is Test, and then it knows that it has to look in class Test for the static member variable.



Hi all,
As Jesper Young has explained that return type will be checked, but the returned value by favourite() method is null, so how come it can access the Test class member? though return expected is Test type. Or is it like this that before reaching to return type(i.e null) it reads the return type in signature and output's the Test's String value?
 
Pranav Bhatt
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any suggestion on my doubt? Please clarify.
[ January 01, 2008: Message edited by: pranav bhatt ]
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by pranav bhatt:


..so how come it can access the Test class member? though return expected is Test type.



What Jesper told is the Compiler looks for the return type in the Signature of the method and NOT inside the method code.

What it bothers about is the Method Signature and NOT the implementation. You may declare a List as a return type but instead you may return an ArrayList. But what matters to the compiler is the List and NOT the actually returned Arraylist.

In this case, had it been any non-static member instead of the static "mountain" string, it would definitely throw a NullPointerException. Since the member being accessed is static, it does NOT require any instance to look into.

Does that clear your doubt?
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Chandrasekhar Mangipudi:
HI. Try this code . It throws nullpointer exception.

public class Test {

String mountain = "Everest";

static Test favorite(){
System.out.print("Mountain");

return null;
}
public static void main(String[] args) {
Test t = new Test();
t=t.favorite();
System.out.println();
System.out.println(t.mountain);
}
}



It would obviously throw a NullPointerException since the "moutain" variable is non-static, as explained above for pranav's query.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Test {
static String mountain = "Everest";

public static void main(String[] args) {
Test t=null;
System.out.println(t.mountain);
}
}


the out put of this is Everest
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy "K rkreddy",

Welcome to JavaRanch

Please adjust your display name according to the Ranch's naming policy .

You can easily update your name by editing your profile.
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by k rkreddy:
public class Test {
static String mountain = "Everest";

public static void main(String[] args) {
Test t=null;
System.out.println(t.mountain);
}
}


the out put of this is Everest



Again, that's because the member being accessed is static and it does NOT really require an active and non-null reference here.
 
Pranav Bhatt
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Raghavan,
Am clear with the concept of Static member accessing and know that it dont need any reference of class to call it. But you either access them with any instance of class(though not right way) or else with the Class name(the correct way to proceed). But k rkreddy example has made it clear.
Thanks
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow!.
Thanks Raghavan for the nice explanation.
And Apratim, for bringing an easy-to-miss subtlety of static members to everyone's attention.. I myself thought it would threw NullPointerException.

[ January 01, 2008: Message edited by: srinadh penugonda ]
[ January 01, 2008: Message edited by: srinadh penugonda ]
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by pranav bhatt:
..it dont need any reference of class to call it.



Obviously it DOES need a class reference and NOT the instance reference That's was all the discussion had been going for!
 
Pranav Bhatt
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ops Sorry i meant it should better be called using class name
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic