This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring in Action (5th edition) and have Craig Walls on-line!
See this thread for details.
Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Class casting/assigning question - REVISED  RSS feed

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Im trying to understand what the Java checks for regarding illegal casts during compile time and runtime.

In this program, I've created four classes, AClass, BClass (which extends AClass), CClass (which extends BClass), and the Tester class. The Tester class creates an object,a, with reference to AClass, but instantiated with CClass. It then creates another object,b, with reference to BClass that holds the value (references the value?) of a copied object a casted into BClass...Finally, the code prints out BClass' i value.

As I understand it...in this statement

AClass a = new CClass(); <--- AClass is reference type and CClass is compile-time type. (Please correct me if wrong)

My Questions Are:
1) What does the Java compiler check for in regards to object assignment and casting? Does it just check to see if compile-time type assignments are valid?


2) What does the Java runtime environment check for in regards to object assignment and casting?


public class Tester {
public static void main(String args[]) {
AClass a = new CClass();
BClass b = (BClass) a;
System.out.println(b.i);
}
}

class AClass {
int i = 5;
void method() {
System.out.println("Class A Method");
}
}

class BClass extends AClass {
int i = 6;
void method() {
System.out.println("Class B Method");
}
}

class CClass extends BClass {
int i = 7;
void method() {
System.out.println("Class C Method");
}
}

I apologize if this question is vague...

Thanks,

Dave
[ April 15, 2005: Message edited by: David Miranda ]
 
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AClass a = new CClass();

AClass is the declared type of the reference, so AClass is the compile-time type. However, the actual object being created is of type CClass, so CClass is the runtime type. The reference to the CClass object is upcast to AClass through "assignment conversion."

What does the Java compiler check for in regards to object assignment and casting? Does it just check to see if compile-time type assignments are valid?

Basically, the compile-time types must have an inheritance relationship. If it's an upcast (or conversion), then no explicit cast is needed because this is always safe. For example, a VWJetta is always a Car. However, if it's a downcast, then an explicit cast is required because this is potentially unsafe. For example, a Car is not always a VWJetta. (If the explicit cast is incorrect, see below...)

What does the Java runtime environment check for in regards to object assignment and casting?

At runtime, the actual runtime type is checked. If the cast is invalid, then a ClassCastException is thrown.
 
David Miranda
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for clearing that up!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!