aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes A method that returns null......... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "A method that returns null........." Watch "A method that returns null........." New topic
Author

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

Nasir Khan
Ranch Hand

Joined: Nov 04, 2000
Posts: 135
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

Joined: Oct 04, 2000
Posts: 45
when the class is loaded all the static variables get assigned/initialised.
Kaushik Subramanya
Greenhorn

Joined: Sep 26, 2000
Posts: 9
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

Joined: Nov 04, 2000
Posts: 135
static variables and static blocks get initialized during class
loading time but not static methods.
Am I right?
Nasir Khan
Ranch Hand

Joined: Nov 04, 2000
Posts: 135
Still waiting.......
Please someone come and solve my problem.....
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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).]


"JavaRanch, where the deer and the Certified play" - David O'Meara
Junaid Bhatra
Ranch Hand

Joined: Jun 27, 2000
Posts: 213
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

Joined: Nov 04, 2000
Posts: 135
Thanks Cindy & Junaid
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A method that returns null.........