File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes why ClassCastException ? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "why ClassCastException ?" Watch "why ClassCastException ?" New topic
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
    
  13

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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why ClassCastException ?
 
Similar Threads
Why this code is not running from command line???
About Strings and memory
String variables and Int variables
Oracle 9i thru WSAD5.0 Problem!!!!!
Trouble Configuring a Data Source for MySQL using JSTL in JBoss