Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dollar dollar= (Dollar) object; What does this do?

Tags:

java

smalltalk

Dollar dollar= (Dollar) object;

What does this snippet in do? Is it even Java? Or Smalltalk? I found it in TDD, which i think was written with Smalltalk in mind.

like image 981
Kaustubh Avatar asked Dec 12 '22 21:12

Kaustubh


2 Answers

This could be Java. It basically casts an object of generic type (perhaps just Object) to a Dollar object.

Example:

Object object = ObjectFactory.getObject(); // Gets object
Dollar dollar = (Dollar) object; // Cast to Dollar object, will throw an exception
                                 // if this isn't possible
dollar.dollarMethod();   // I can now call Dollar methods
like image 88
kgiannakakis Avatar answered Dec 31 '22 05:12

kgiannakakis


It creates a new variable of class Dollar, with the name dollar. Then it assigns a value to that variable by casting to Dollar a variable named object. It's valid Java code, providing there's a class named Dollar defined. But if the variable named object is not of class Dollar it might throw a ClassCastException.

like image 38
Andrei Fierbinteanu Avatar answered Dec 31 '22 06:12

Andrei Fierbinteanu