Riduidel
Riduidel

Reputation: 22292

Is it possible to put several groovy classes in the same groovy file?

You know and I know it is possible to do in Java, provided only one is public. But, is it possible to do so in Groovy ? And if so under which conditions ?

Upvotes: 10

Views: 6800

Answers (2)

Dónal
Dónal

Reputation: 187529

The differences between Java and Groovy in terms of which classes you can put in a single file are:

  • MyFile.groovy can have multiple public classes, whereas MyFile.java can only have one
  • MyFile.java must have a class MyFile, whereas there is no such requirement for MyFile.groovy

Upvotes: 10

Booyeoo
Booyeoo

Reputation: 583

public class A{
Integer a=2
}

public class B{
Integer b=3+new CB().cb
}

private class CB{
Integer cb=2
}

assert new A().a+new B().b==7

Yes you can put them all in one file and just use them as you want in you main task... or what do you mean by "which conditions" ?

Upvotes: 6

Related Questions