• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Is this possible in Java?

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 🙂
 
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.
 
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.
 
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)
 
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:
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).
 
Back
Top