• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Doubt in return value

 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

public class returnNull
{
static String myName="SCJP";
public returnNull getMyClass()
{
System.out.println(myName);
return null;//line1
}
public static void main(String args[])
{
System.out.println(new returnNull().getMyClass().myName);
}
}


Here i thought it will give a compiler error because the return type in line 1 is not matching with the required return type.

I.e required is :returnNull
Found: null.

Please explain what is happening here

Thanks in advance
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
null is not a type.
it is kind of an "empty object" that can have any type.
Therefore it is possible to return null in your method getMyClass()

getMyClass() returns an instance of class returnNull.
And since this class has got a String variable "myName", you can say:

new returnNull().getMyClass().myName

For the compiler this is fine, only you get a null pointer exception at runtime because the middle part returns null and in the right part you try to access a variable on the null.



Yours,
Bu.
[ September 17, 2007: Message edited by: Burkhard Hassel ]
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

The very odd thing is that when I tried the above code, it didn't throw a NullPointerException. It printed:
SCJP
SCJP
I presume it is to do with the fact that myName is static.

I then added the following at the end of main(...)



which made the output:

SCJP
SCJP
SCJP

No NullPointerException


I hope this helps
Katrin
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static variables are usually accessed in the following way :
className.variableName.

It doesn't matter to the compiler if you are trying to use an instance to access a static variable. It just ignores any instance and uses className.variableName as static variables are associated with the class, rather than with any object
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Katrin,
you're right with the static.

I overlooked that mighty modifier...


Yours,
Bu.
 
What does a metric clock look like? I bet it is nothing like this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic