Skip to content

Instantly share code, notes, and snippets.

@ppazos
Forked from neelsmith/instanceFromName.groovy
Created February 11, 2024 15:52
Show Gist options
  • Save ppazos/54bce66b78a9265ea69ce35541daa395 to your computer and use it in GitHub Desktop.
Save ppazos/54bce66b78a9265ea69ce35541daa395 to your computer and use it in GitHub Desktop.
groovy: get an instance of a class from its name as a string
def objectInstance = Class.forName("NAME.AS.STRING").newInstance()
@ppazos
Copy link
Author

ppazos commented Feb 11, 2024

class Dog {

   def bark()
   {
      println 'woof'
   }
}


def name = 'Dog'

def ins = Class.forName(name).newInstance()

ins.bark()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment