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 / November 2005

Tip: Looking for answers? Try searching our database.

object.getClass.cast(object) ?

Thread view: 
toggles@gmail.com - 23 Nov 2005 16:13 GMT
When I run the java code below I get this:

uprrtoa@uprags011> java Test
Class : class Test$C
cast(): Test$C@192d342

This tells me that the cast() call is working and finding the right
class, but if you uncomment the line:

  dispatch(a.getClass().cast(a));

and try and compile it I get the following:

uprrtoa@uprags011> javac Test.java
Test.java:44: cannot find symbol
symbol  : method dispatch(Test.A)
location: class Test
       dispatch(a.getClass().cast(a));
       ^
1 error

it's looking for a method with the signature:

void dispatch(A a);

which of course isn't the desired result.

Any ideas? or does my lack of understanding of generics show through?

Cheers!

/*
* Test.java
*
* Created on November 23, 2005, 10:42 AM
*/

/**
*
* @author  toggles
*/
public class Test {

   public class A {
       public A() {
       }
   }

   public class B extends A {
       public B() {
           super();
       }
   }

   public class C extends A {
       public C() {
           super();
       }
   }

   public void dispatch(B b) {
       System.out.println("B");
   }

   public void dispatch(C c) {
       System.out.println("C");
   }

   /** Creates a new instance of Test */
   public Test() {
       A a=new C();

       System.out.println("Class : " + a.getClass());
       System.out.println("cast(): " + a.getClass().cast(a));
       //dispatch(a.getClass().cast(a));
   }

   /**
    * @param args the command line arguments
    */
   public static void main(String[] args) {
       Test t=new Test();
   }
   
}
Viator - 23 Nov 2005 17:03 GMT
What is that method cast()??? Is it a member of Class class??? I do not
think so!!

Amit :-)
toggles@gmail.com - 23 Nov 2005 17:29 GMT
Actually, yes it is a member of the Class class.

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Class.html

or more specificaly:
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Class.html#cast(java.lang.Object)
Viator - 24 Nov 2005 14:03 GMT
Sorry I am not too much into Java 5.

Amit :-)
Thomas Hawtin - 23 Nov 2005 18:19 GMT
> uprrtoa@uprags011> java Test
> Class : class Test$C
[quoted text clipped - 14 lines]
>         ^
> 1 error

The compile time type of a is Test.A, and its runtime type is Test.C.
The result of a.getClass() is Test.C.class, but the compile type type is
Class<? extends Test.A>. The compile time result type of Class<? extends
Test.A>.cast is Test.A. There is not suitable dispatch method, so you
get a compiler error.

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

toggles@gmail.com - 23 Nov 2005 19:23 GMT
Thanks Tom, thats kind of what I figured but if you add the method

public void dispatch(A a) {
       System.out.println("A");
}

Then thats the method that gets called, which seems to me to be
incorrect, as at runtime the result of the cast() call should be
Test.C, thus dispatch(C c); should be called? or am I just missing the
entire point of the cast() method?

Thanks again, I appreciate the help.
Thomas Hawtin - 23 Nov 2005 19:34 GMT
> Thanks Tom, thats kind of what I figured but if you add the method
>
[quoted text clipped - 6 lines]
> Test.C, thus dispatch(C c); should be called? or am I just missing the
> entire point of the cast() method?

I think you're missing the whole point of method overloading.
Overloading is done on the basis of compile-time types.

The result of the cast call is a, statically typed as Test.A. All the
cast method has done was a runtime check that a is of runtime type Test.C.

The entire point of Class.cast is to do runtime checks, not compile-time
check.

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/



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.