| Author |
why ClassCastException ?
|
siv sanny
Greenhorn
Joined: Jan 03, 2005
Posts: 8
|
|
See this code, I think its simple to understand; Here are my questions 1. since there are two instances of Same Class, which will be considered when i refered that class(creating a new instance or acceing a member)? 2. How can i access data in the second case ? O/p data ===> 1234 java.net.URLClassLoader@422ede java.lang.ClassCastException at SMT.main(SMT.java:39) thanks -ssv [ August 10, 2005: Message edited by: sankar vas ]
|
 |
Joshua Bloch
Author and "Sun God"
Ranch Hand
Joined: May 30, 2001
Posts: 124
|
|
Sankar, The problem is that you have two different, unrelated classes, both named SMT. One is the class whose source code you've provided and the other one is the one that you load explicitly with this line: The name of a class consists of three parts: the simple (unqualified) name, the package name, and the class loader. In this case, you have two classes with the same simple name and package names, but different class loader names. They are unlrelated. The comment "//reload" is wrong. The original class is still there. A wise man once said "When the code and the comment disagree, both are probably wrong " Regards, Josh
|
Joshua Bloch <br />Author of <a href="http://www.amazon.com/exec/obidos/ASIN/0201310058/ref=ase_electricporkchop" target="_blank" rel="nofollow">Effective Java</a> and coauthor of <a href="http://www.amazon.com/exec/obidos/ASIN/032133678X/ref=ase_electricporkchop" target="_blank" rel="nofollow">Java Puzzlers</a>
|
 |
Norm Radder
Ranch Hand
Joined: Aug 10, 2005
Posts: 681
|
|
I don't get the same error? Probably Classpath? import java.util.*; import java.net.*; public class SMT { public static int data = 1234; public static void main(String[] args) throws Exception { // code to set SecurityManager System.out.println("data ===>" + data ); data = 9999999; URL[] url = {new URL("file://D:/Myworkspace/")};//url of SMT Class Class c = new URLClassLoader(url, null).loadClass("SMT");//reload System.out.println(c.getClassLoader()); Object o = c.newInstance(); System.out.println(((SMT)o).data ); } }/* Output when executed: Running: java SMT data ===>1234 java.lang.ClassNotFoundException: SMT at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at SMT.main(SMT.java) Exception in thread "main" 0 error(s) */
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24041
|
|
Originally posted by Norm Radder: URL[] url = {new URL("file://D:/Myworkspace/")};//url of SMT Class
Unless your copy of SMT.class is in this same directory, then yes. This has to be changed to match your setup.
|
[Jess in Action][AskingGoodQuestions]
|
 |
siv sanny
Greenhorn
Joined: Jan 03, 2005
Posts: 8
|
|
The name of a class consists of three parts: the simple (unqualified) name, the package name, and the class loader. In this case, you have two classes with the same simple name and package names, but different class loader names. They are unlrelated. The comment "//reload" is wrong. The original class is still there. A wise man once said "When the code and the comment disagree, both are probably wrong " Regards, Josh[/qb]<hr></blockquote> thanks very much for your comments. however, i am still not clear. let me put my question clearly : here i have two class loaders, one system class loader and one my own loader, loading one class SMT twice, so when i try to cast the object returned by my class loader it gives me a exception ...so how am i supposed to cast it ???... do i say ((myclassLoader.SMT)o).data ??? or something like that ??? how do i identify a class with those 3 things classloader.packageName.className ??? point to note : i have given 'null' as parent class loader to my class loader. [ August 11, 2005: Message edited by: sankar vas ]
|
 |
 |
|
|
subject: why ClassCastException ?
|
|
|