File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Nasir Khan
Ranch Hand
Posts: 135
  • 0
  • Mark post as helpful
  • send pies
  • 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....
 
Anbooo Sanygao
Ranch Hand
Posts: 45
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when the class is loaded all the static variables get assigned/initialised.
 
Kaushik Subramanya
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still waiting.......
Please someone come and solve my problem.....
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • 0
  • Mark post as helpful
  • send pies
  • 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).]
 
Junaid Bhatra
Ranch Hand
Posts: 213
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Cindy & Junaid
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic