Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find out the declared type of an identifier in Java?

I have a simple class Apple extends from another simple class Fruit.

At run-time, I could use

Fruit fruit = new Apple();

fruit.getClass();

to get the actual type of fruit object, which is Apple.class.

I could also use fruit instanceof Apple, and fruit instanceof Fruit to verify if this fruit object is an instance of Apple or Fruit. Both of these 2 expressions return true, which is normal.

But is there a way to determine precisely the declared type of fruit identifier? Which in this case is Fruit.

like image 632
Kewei Shang Avatar asked Jul 07 '12 12:07

Kewei Shang


People also ask

What is declared Type in Java?

The declared type or compile-time type of a variable is the type that is used in the declaration. The run-time type or actual type is the class that actually creates the object. The variable nameList declared below has a declared type of List and an actual or run-time type of ArrayList .

What are the rules for declaring an identifier in Java?

Rules for defining Java IdentifiersThe only allowed characters for identifiers are all alphanumeric characters([A-Z],[a-z],[0-9]), '$'(dollar sign) and '_' (underscore). For example “geek@” is not a valid java identifier as it contain '@' special character. Identifiers should not start with digits([0-9]).

What are the identifiers in Java?

Identifiers in Java are names that distinguish between different Java entities, such as classes, methods, variables, and packages. Identifiers include the names of classes, methods, variables, packages, constants, etc. These identifiers are each specified using a specific syntax and naming scheme.


1 Answers

You're actually asking a question about the variable declaration of fruit rather than the actual runtime type of the object (which is an Apple in this case).

I think this is in general a bad idea: you just declared the variable and told the compiler that it is a Fruit, so why do you need to now need to find this out?

Just to confuse matters even more, it's worth noting that you can also have multiple variables with different declared types referencing the same object (which is still an Apple):

Fruit fruit = new Apple(); // fruit declared as Fruit, but refers to an Apple
Object thing = fruit;      // thing declared as Object, refers to the same Apple

If you really want to find out the declared type, then you have a few options:

  • Make fruit an instance variable, and query the declared type using reflection.
  • Do some processing of the source code to find the variable declaration
  • Do some processing of the compiled bytecode to find the declaration type (although there is a possibility that an aggressive compiler might even optimise the compile time declaration away altogether, e.g. after realising that fruit can only ever be an Apple in this code)

I think all of these are pretty ugly, so my general advice would be "don't do it".

like image 83
mikera Avatar answered Sep 29 '22 12:09

mikera