Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groovy code - import statement not needed?

Tags:

java

groovy

I created a groovy class with a method that returns Collection<String>. It works, but it's weird that there is no Collection in the import statements.

The class inherits a super class. That super class (java) does have the import statement for collection: import java.util.Collection;

Is it the expected behavior?

Does java or groovy inherit imports too? I doubt that.

like image 566
ND27 Avatar asked Jul 09 '14 19:07

ND27


People also ask

Why does import statement need in OOP?

Import statement in Java is helpful to take a class or all classes visible for a program specified under a package, with the help of a single statement. It is pretty beneficial as the programmer do not require to write the entire class definition. Hence, it improves the readability of the program.

How do I import into groovy?

A simple import is an import statement where you fully define the class name along with the package. For example the import statement import groovy. xml. MarkupBuilder in the code below is a simple import which directly refers to a class inside a package.

Is import statement mandatory in Java?

The import statement is optional in Java. If you want to use class/interface from a certain package, you can also use its fully qualified name, which includes its full package hierarchy. Here is an example to import a package using the import statement.

What does [:] mean in groovy?

[:] creates an empty Map. The colon is there to distinguish it from [] , which creates an empty List. This groovy code: def foo = [:]


2 Answers

No imports are not inherited between classes.

In Groovy all of the below packages are imported by default.

  • java.io.*
  • java.lang.*
  • java.math.BigDecimal
  • java.math.BigInteger
  • java.net.*
  • java.util.* (which includes Collection)
  • groovy.lang.*
  • groovy.util.*
like image 92
M A Avatar answered Sep 29 '22 08:09

M A


Have a look at default imports. This is expected behavior. There's no import inheritance. All these packages are imported for every class.

like image 37
Opal Avatar answered Sep 29 '22 06:09

Opal