Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2007

Tip: Looking for answers? Try searching our database.

Determine type of a variable whose value is NULL

Thread view: 
brian.vanheesch@gmail.com - 14 Sep 2007 13:46 GMT
I have a variable defined (local, global, parameter, field; it does
matter) such as:

MyClass abc=null;
MyClass def=new MyClass();

The variables above will house an object of type MyClass or any
subclass.  Now in code, I would like to determine the specific type of
the variable.  So I can do:

System.out.println(def.getClass().getName());

but what can I do for variable 'abc' ?  Using the same syntax for
variable abc, will naturally result in an NullPointerException.

Is there a typeof syntax similar to instanceof?
Lew - 14 Sep 2007 14:32 GMT
> I have a variable defined (local, global, parameter, field; it does
> matter) such as:
[quoted text clipped - 12 lines]
>
> Is there a typeof syntax similar to instanceof?

No.  Why does instanceof not help you?

<http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.20.2>

abc can be tested with the instanceof operator, in which case null will return
fallse for (abc instanceof MyClass), but more usefully just test it for null
first, as in
  if ( abc == null )
  {
   // handle null
  }
  else ...

The literal null has only one type.
<http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.1>
> There is also a special /null/ type, the type of the expression null, which has no name.
[emph. orig.]

Signature

Lew

Mike Schilling - 14 Sep 2007 15:04 GMT
>I have a variable defined (local, global, parameter, field; it does
> matter) such as:
[quoted text clipped - 9 lines]
>
> but what can I do for variable 'abc' ?

System.out.println( abc == null ? "<null>" : abc.getClass().getName());
Roedy Green - 15 Sep 2007 20:23 GMT
>Is there a typeof syntax similar to instanceof?

null variables don't have a class.

You can do something like this

Skurtlewax w = null;

System.out.println( Skurltewax.class );
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.