0

A downcast can change the dynamic type of an object.

Why is this statement false? Is that because, there aren't static and dynamic types in Java?

Moreover, what is the static type and dynamic types of an object?

Tom
  • 16,842
  • 17
  • 45
  • 54
codemonkey
  • 331
  • 1
  • 4
  • 16
  • 1
    `A a = new B();` Where `A` static and `B` is dynamic. – Suresh Atta Sep 25 '15 at 18:06
  • 6
    "static type" is what the compiler knows about a _variable_. "dynamic type" is the actual type of an _object._ A Java object is what it is: There is no way to change it's type, ever. – Solomon Slow Sep 25 '15 at 18:11
  • 1
    Dynamic type cannot be changed either by downcasting or upcasting. The dynamic type *is* the type – ControlAltDel Sep 25 '15 at 18:13
  • 1
    You cannot change the type of an object. You can only change the type of reference variable pointing to it – Vivin Sep 25 '15 at 18:18

1 Answers1

0

As @ControlAltDel stated, the type of the object is a type. Downcasting doesn't change anything.

Once created, the classes type doesn't change. No way to do that in java.

The static type is the type of the reference to the variable that you are holding. The dynamic type is the instance that it points to.

You can change the object that's pointed to by the reference and move up and down the hierarchy, but can't ever change the type of the instance.

Thom
  • 14,013
  • 25
  • 105
  • 185