Win a copy of Terraform in Action this week in the Cloud forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

A method that returns null.........

 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have a problem with this code
_________________________
class X{static int i=5;} //line 1

class Y{
static X method(){return null;}//line 2
public static void main(String st[]){
int g=method().i;
System.out.println(g);
}
}
__________________________
Obviously there should be a NullpointerException
but its giving an output of 5
but when i remove static either from line1 or line2
there is NullpointerException.....
Please help me on this....
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when the class is loaded all the static variables get assigned/initialised.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nasir,
Static variables are automatically initialized.
Hope that helps
Kaushik

Originally posted by Nasir Khan:
Hi,
I have a problem with this code
_________________________
class X{static int i=5;} //line 1

class Y{
static X method(){return null;}//line 2
public static void main(String st[]){
int g=method().i;
System.out.println(g);
}
}
__________________________
Obviously there should be a NullpointerException
but its giving an output of 5
but when i remove static either from line1 or line2
there is NullpointerException.....
Please help me on this....


 
Nasir Khan
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
static variables and static blocks get initialized during class
loading time but not static methods.
Am I right?
 
Nasir Khan
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Still waiting.......
Please someone come and solve my problem.....
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static methods can be used even when there are no instances of the class created. Therefore when you use the method() class it can access the static i variable immediately even though there are no instances of Y created.
The method is using the X return type, even though it is set to null to get at the static variable.
[This message has been edited by Cindy Glass (edited November 29, 2000).]
[This message has been edited by Cindy Glass (edited November 29, 2000).]
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer to this question is that static methods and variables are resolved at compile-time. Thus when i is static, the compiler does not see the type returned by method(), which is null. It just makes the call X.i

Originally posted by Nasir Khan:
Hi,
I have a problem with this code
_________________________
class X{static int i=5;} //line 1

class Y{
static X method(){return null;}//line 2
public static void main(String st[]){
int g=method().i;
System.out.println(g);
}
}
__________________________
Obviously there should be a NullpointerException
but its giving an output of 5
but when i remove static either from line1 or line2
there is NullpointerException.....
Please help me on this....


 
Nasir Khan
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Cindy & Junaid
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic