Search the archives!
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Advanced-java] Basic class question
- From: vladimir@xxxxxxx (Vladimir G Ivanovic)
- Subject: [Advanced-java] Basic class question
- Date: Wed, 27 Feb 2002 12:25:52 -0800
"JC" == Justin Couch <justin@xxxxxxxxxx> writes:
JC> Sybille Breunig wrote:
>> Why don't you use :
>> if (obj instanceof X)
>> // do anything
JC> Because that will always evaluate to false in this case. instanceof is a
JC> runtime operator that acts on the class. If you have a variable of type
JC> Class then having some random object checking for the class won't
JC> work.
I'm sorry, I'm confused because "obj instanceof X" will not always
evaluate to false unless obj is *never* of type X. This is clearly not
the case here. What were you trying to say? (Also note that "instanceof"
is an operator that acts on an *instance* of a class, not on a class.)
JC> void checker(Object blah) {
JC> Class myClass = foo.class;
JC> if(blah instanceof myClass)
JC> ///
JC> }
JC> This will not compile.
Agreed, but no one said that it would. Are you just iterating over a
list of all possible forms, or is there some point I'm missing?
JC> The RHS of instanceof must be a name of a class,
JC> not the Class object. If you want to check that two objects are of the
JC> same class, when one of those objects is expressed in terms of an
JC> instance of java.lang.Class, the only way you can do it is through the
JC> isInstance() method. This will check for any object being of the same
JC> class type. ie, it evaluates to the equivalent of
JC> void checker(Object blah) {
JC> Class myClass = foo.class;
JC> if(myClass.equals(blah.getClass())
JC> ///
JC> }
Agreed, but this doesn't really answer dovle's original questions, "What
is the best way of checking to see if a given object is the member of a
particular class?"
Both
if (obj.getClass() == xClass) ....
and
if (obj instanceof X) ...
will work.
class X {}
class ClassTest {
public static void main(String[] args) {
Class xClass = X.class;
String s = new String("foo");
X obj = new X();
if (obj.getClass() == xClass)
System.out.println("obj.getClass() == xClass ==> true");
if (s instanceof String)
System.out.println("s instanceof String ==> true");
}
}
===>
obj.getClass() == xClass ==> true
s instanceof String ==> true
Unless the class membership test expression is in the middle of a loop
that consumes most of the application's cycles, I wouldn't worry about
which is more efficient. It's much better to get the rest of a program
right than to worry about minor inefficiencies.
To answer dovle's other questions:
There is one class object per loaded class per class loader. (I.e., two
classes with the same name loaded by different class loaders are
distinct classes with distinct class objects.) Many programs only use
the system class loader, so this reduces to "there is only one class
object per loaded class".
Class objects may be garbage collected if their defining class loader is
no longer reachable, but this is an optional capability in JVMs. I don't
know if it's implemented in Sun's Java 1.4.0, for instance.
--- Vladimir
References:
* "Dynamic Class Loading in the Java Virtual Machine", S.Liang & G.Bracha,
OOPSLA'98 (http://citeseer.nj.nec.com/liang98dynamic.html)
* Java Language Spec, 2/e, Section 2.17.8 Unloading of Classes and Interfaces
(http://java.sun.com/docs/books/vmspec/2nd-edition/html/Concepts.doc.html#32203)
--------
Vladimir G. Ivanovic http://leonora.org/~vladimir
2770 Cowper St. vladimir@xxxxxxx
Palo Alto, CA 94306-2447 +1 650 678 8014
- Follow-Ups:
- [Advanced-java] Basic class question
- From: Justin Couch
- [Advanced-java] Basic class question
- References:
- [Advanced-java] BAsic class question
- From: Justin Couch
- [Advanced-java] BAsic class question
- Prev by Date: [Advanced-java] problems with sql sentence
- Next by Date: [Advanced-java] Basic class question
- Previous by thread: [Advanced-java] BAsic class question
- Next by thread: [Advanced-java] Basic class question
- Index(es):