Is this possible in Java?

Maximilian

Lifer
Feb 8, 2004
12,604
15
81
To declare a variable of a variable type? I dont know a better way to word it. For example declaring a variable like this (which dosent work it seems but maybe theres another way):

Code:
anObject.getClass() variableName = anObject;

as opposed to:

Code:
String variableName = anObject;

Im just tinkering around with objects at the moment, ive found out that I can stuff an array full of different object types as long as it is an Object[]. When taking them out I can use the Object.getClass() to see what it actually is, pretty cool :)
 

BrightCandle

Diamond Member
Mar 15, 2007
4,762
0
76
You can't, but there is a good reason for it. The closest you can get is
Code:
Object variableName = anObject;

If you think about this logically the whole point of having a more specific class associated with the variable is that you can then use its specific interface, ie call its methods or perhaps access its variables. But if the type of it isn't fixed then you can't create valid code because you could be calling onto a method that doesn't exist on the actual runtime time of the object.

So actually the way you go about doing this is to use Object initially, then test the type and then call specific code:

Code:
Object variableName = anObject;

if(variableName instanceof MyInterface) {
... //call specific code on MyInterface
}

But it doesn't make any sense to do what you are talking about in a compiled language, it would gain you very little.
 

Fox5

Diamond Member
Jan 31, 2005
5,957
7
81
Yeah just make it an Object, then test it via instanceof, then cast it to what you want.
 

KB

Diamond Member
Nov 8, 1999
5,406
389
126
Usually if you have a list of different class objects, you implement an interface each of the class will support. This allows you to call the same methods on all of the different class objects.
 

Maximilian

Lifer
Feb 8, 2004
12,604
15
81
You can't, but there is a good reason for it. The closest you can get is
Code:
Object variableName = anObject;

If you think about this logically the whole point of having a more specific class associated with the variable is that you can then use its specific interface, ie call its methods or perhaps access its variables. But if the type of it isn't fixed then you can't create valid code because you could be calling onto a method that doesn't exist on the actual runtime time of the object.

So actually the way you go about doing this is to use Object initially, then test the type and then call specific code:

Code:
Object variableName = anObject;

if(variableName instanceof MyInterface) {
... //call specific code on MyInterface
}

But it doesn't make any sense to do what you are talking about in a compiled language, it would gain you very little.

Yeah that makes a lot of sense actually :awe:

Ive never seen "instanceof" before, is that the best way to check if an object is of a certain type?

I was doing this previously:
Code:
if(anObject.getClass().equals(Class.forName("java.lang.StringBuilder")))

Your instanceof seems a lot cleaner:
Code:
if(anObject instanceof StringBuilder)
 

BrightCandle

Diamond Member
Mar 15, 2007
4,762
0
76
Instanceof is significantly faster, but its also not the same thing. It checks that is exactly the class or its subclass, or an implementer of the interface. If the purpose is checking the actual interface is valid for casting the object then its the right thing to use, if you need to know an object is an exact type then it isn't the right thing.

Its a keyword, it produces very specific bytecode for this purpose, its part of the JVM.
 
Last edited:

Cogman

Lifer
Sep 19, 2000
10,284
138
106
IMO instanceof is a code smell in java. I get that it is sometimes useful, but it usually occurs in code that has dragons.

Bright candle is right, insanceof is much faster and is doing something different, but the slow part of your code is class.forName. If you cached the class you are looking for, then you can beat instanceof performance with equals or == (the same in the case of java Class objects).