Thread View: comp.lang.java.programmer
4 messages
4 total messages
Started by Carl Hewitt
Sat, 29 Nov 1997 00:00
Bug in Class class?????
Author: Carl Hewitt
Date: Sat, 29 Nov 1997 00:00
Date: Sat, 29 Nov 1997 00:00
29 lines
1128 bytes
1128 bytes
The isInstance method of the Class class does not seem to work (as does
the
getClasses() and getDeclaredClasses() The following code returns false
for all three statements)
TIA
Carl
public class MyClass1 {
public static void main(String[] args) {
System.out.println(b.class.isInstance(a.class));
System.out.println(a.class.isInstance(a1.class));
System.out.println(b.class.isInstance(a1.class));
}
}
class a implements a1 {
}
class b extends a {
}
interface a1 {
}
Re: Bug in Class class?????
Author: Will Wood
Date: Sat, 29 Nov 1997 00:00
Date: Sat, 29 Nov 1997 00:00
19 lines
974 bytes
974 bytes
I haven't checked out isInstance but getClasses and getDeclaredClasses are not implemented in JDK 1.1 even though they are documented as being there. They return an empty array... -- Will Wood <wkwood@burgoyne.com> Carl Hewitt <carl@oop.com> wrote in article <34801D17.4EA42334@oop.com>... SNIP > >
Re: Bug in Class class?????
Author: Pete Cockerell
Date: Sat, 29 Nov 1997 00:00
Date: Sat, 29 Nov 1997 00:00
65 lines
2545 bytes
2545 bytes
I'm not sure that you've wholly grasped what isInstance() does. The
argument is an instance of Object (not of class Class), and the
function returns true if the argument is an instance of the receiver
Class object, or one of its subclasses. In your example, you'll never
get 'true' because your arguments are all of class Class. What you
want is:
System.out.println(b.class.isInstance(new a()));
System.out.println(a.class.isInstance(new a1()));
System.out.println(b.class.isInstance(new a1()));
If you think about it, there would be no need for a function to
provide the information you're asking for, because it's statically
determined by the class hierachy. For example, this would return true:
System.out.println(Class.class.isInstance(a.class));
but everything in that expression is knowable at compile time, so it's
just the same as saying
System.out.println(a.class instanceof Class);
isInstance() is designed to return information about the dynamic class
of instances, which are typically referenced by variables of some
high-up superclass, e.g. Object. Though of course, in general, asking
an object if it's an instance of a particular class is a Bad Thing and
leads to fragile code.
Cheers,
Pete
Carl Hewitt wrote:
>
> The isInstance method of the Class class does not seem to work (as does the
> getClasses() and getDeclaredClasses() The following code returns false
> for all three statements)
>
> TIA
>
> Carl
>
> public class MyClass1 {
> public static void main(String[] args) {
> System.out.println(b.class.isInstance(a.class));
> System.out.println(a.class.isInstance(a1.class));
> System.out.println(b.class.isInstance(a1.class));
> }
> }
>
> class a implements a1 {
>
> }
>
> class b extends a {
>
> }
>
> interface a1 {
>
> }
--
Pete Cockerell
California, USA
Re: Bug in Class class?????
Author: rudnicki
Date: Sat, 29 Nov 1997 00:00
Date: Sat, 29 Nov 1997 00:00
112 lines
4111 bytes
4111 bytes
Carl Hewitt wrote:
>
> The isInstance method of the Class class does not seem to work (as does
> the getClasses() and getDeclaredClasses() The following code returns false
> for all three statements)
>
> public class MyClass1 {
> public static void main(String[] args) {
> System.out.println(b.class.isInstance(a.class));
> System.out.println(a.class.isInstance(a1.class));
> System.out.println(b.class.isInstance(a1.class));
> }
> }
Carl, I did some experimenting. I think the .class operator (what is
that thing? a
member containing this?) does indeed work as seen if you run the snip
below. It seems
reasonable that the Class objects that are created by the loader are not
instances of each
other.
A someone else mentioned, the question the above code is asking is known
at compile time. That
means you should know. There is use for compiler directives (e.g.
sizeof() in C) but maybe the
class heirarchy can't be explored without creating an instance of the
class. Given the way classes
are dynamically loaded, this makes sense.
hope this helps,
Jim
public static void main(String[] args)
{
Class_a instance_a = null;
Class_b instance_b = null;
Object instance_x = null;
Object instance_y = null;
/* all false - a sub class is not an instance of.
From my copy of Java in a nutshell -
"There is one Class object for each class that is
loaded into the Java Virtual Machine."
Thereby, Class_x.class returns a reference to a _Class_ object.
This reference gets passed to the isInstance() function.*/
System.out.print("All false ");
System.out.print(Class_b.class.isInstance(Class_a.class));
System.out.print(Class_a.class.isInstance(Class_a1.class));
System.out.println(Class_b.class.isInstance(Class_a1.class));
instance_a = new Class_a();
instance_b = new Class_b();
System.out.print("A is A B is B? (y y) ");
System.out.print(instance_b.getClass().isInstance(instance_b2));
System.out.print(" ");
System.out.println(instance_a.getClass().isInstance(instance_a2));
System.out.print("B is a A ? (y y) ");
System.out.print(instance_a.getClass().isInstance(instance_b));
System.out.print(" ");
System.out.println(Class_a.class.isInstance(instance_b));
System.out.print("A is a B ? (n n) ");
System.out.print(instance_b.getClass().isInstance(instance_a));
System.out.print(" ");
System.out.println(Class_b.class.isInstance(instance_a));
instance_x = (Object)instance_a;
instance_y = (Object)instance_b;
System.out.println("Set instance_x = instance_a");
System.out.println("Set instance_y = instance_b");
System.out.print("instance x is Class_a ? (y) ");
System.out.println(Class_a.class.isInstance(instance_x));
System.out.print("instance x is Class_b ? (n) ");
System.out.println(Class_b.class.isInstance(instance_x));
System.out.print("instance y is Class_b ? (y) ");
System.out.println(Class_b.class.isInstance(instance_y));
System.out.print("instance y is Class_a ? (y) ");
System.out.println(Class_a.class.isInstance(instance_y));
System.out.print("instance y is Class_a1 ? (y) ");
System.out.println(Class_a1.class.isInstance(instance_y));
return;
}
}
class Class_a implements Class_a1
{
public Class_a()
{
}
}
class Class_b extends Class_a
{
public Class_b()
{}
}
interface Class_a1
{
}
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads