i heard that these(say for example Groovy) languages have the capability of changing the variable name or call methods dynamically in runtime! What you meant by dynamic languages? And what is the real need for changing any values during runtime? Is that doen't lead to confusion, because at runtime if you change any value(or your programming constriants change anything), then whats the need for compilation(because it decides and confirms these values will be used, then there is no meaning of changing it in dynamically)? And i know there should be something useful, so only people have introduced these concepts!

I guess i'm clear about my question! And i need some brief explanation :)

2 Answers 2


Dynamic languages give you flexibility and expressiveness in your code. But can also make things difficult to follow sometimes, and definately opens up the potentials of abuse as @TheLQ points out but it has its benefits as well.

As the question was around groovy, I want to point out that there is a guide specifically on Dynamic Groovy.

First of all, Using metaclasses I can augment my classes at runtime to change behaviours of individual instances or of all instances that will ever be declared, even static methods. These metaclasses can be used for convience (think extending final classes with methods that you've always wanted... Groovy in fact has a number of convient augmentations all of their classes), and is also handy for mocking parts of classes for various tests or hacks you need.

Additionally you can implement methodMissing and propertyMissing to use the signature of the method invoked to dynamically drive content. For example, GORM has Dynamic Finders takes advantage of this to make querying more concise. Instead of having to construct a HSQL query, or other hoops I can do object.findAllNameAndVersion("name",123) As a side note Rails also has this ability.

Hope this helps!

  • 1
    No problem @Anto :) As I thought about it more, some additional benefits that the dynamic language provides that you may be interested in. Starting with some of the above, mix in Groovy's implementation of Closures, and you have the basis for some slick Builder patterns (for example JsonBuilder and MarkupBuilder), and the ability to quickly make other DSLs
    – Charlie
    Mar 5, 2011 at 20:50

The only thing I can think of that comes close to what your talking about is PHP's (horrible) variable variables.

$var = "foo";
$$var = "bar";
echo $var; //echo's foo
echo $foo; //echo's bar
echo ${$var}; //echo's bar

The "dynamicness" of this is if $var was assigned a value, for example, from input, the variable for "bar" would be completely different.

As a side note, don't ever do this


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.