Fork me on GitHub

Anonymous Class

Anonymous classes enable you to make your code more concise. They enable you to declare and instantiate a class at the same time. They are like local classes except that they do not have a name. Use them if you need to use a local class only once.

1. Declaring Anonymous Classes

While local classes are class declarations, anonymous classes are expressions, which means that you define the class in another expression.

The following example, HelloWorldAnonymousClasses, uses anonymous classes in the initialization statements of the local variables frenchGreeting and spanishGreeting:

interface HelloWorld {
    def greetSomeone(someone : String)
}
class HelloWorldAnonymousClasses {
    def sayHello {
        var frenchGreeting = new HelloWorld {
            var name = "tout le monde"
            def greetSomeone(someone : String) {
                name = someone
                println("Salut " + name)
            }
        }

        var spanishGreeting = new HelloWorld {
            var name = "mundo"
            def greetSomeone(someone : String) {
                name = someone
                println("Hola, " + name)
            }
        }
        frenchGreeting.greetSomeone("Marc")
        spanishGreeting.greetSomeone("Marco")
    }
}

2. Syntax of Anonymous Classes

As mentioned previously, an anonymous class is an expression. The syntax of an anonymous class expression is like the invocation of a constructor, except that there is a class definition contained in a block of code.

Consider the instantiation of the frenchGreeting object ion the code below.

The anonymous class expression consists of the following:

Because an anonymous class definition is an expression, it must be part of a statement. In this example, the anonymous class expression is part of the statement that instantiates the frenchGreeting object.

var frenchGreeting = new HelloWorld {
    var name = "tout le monde"
    def greetSomeone(someone : String) {
        name = someone
        println("Salut " + name)
    }
}

3. Accessing Local Variables of the Enclosing Scope, and Declaring and Accessing Members of the Anonymous Class

Anonymous classes can capture variables; they have the same access to local variables of the enclosing scope:

Anonymous classes have restrictions with respect to their members:

Note You can declare the following in anonymous classes: fields, extra methods (even if they do not implement any methods of the supertype), instance initializers, local classes. However, you cannot declare constructors in an anonymous class.

class HelloWorldAnonymousClasses {
	var name : String
	def sayHello {
		var frenchGreeting = new HelloWorld {
			def greetSomeone(someone : String) {
				name = someone
				println("Salut " + name)
			}
		}
	}
}

4. References

This documentation is based on documentations from the Xtext and Xtend projects, and from the Java tutorials. Thank you to the contributors to these documents.

5. Version Specification

Copyright © 2014-2025 SARL.io, the Original Authors and Main Authors.

Documentation text and medias are licensed under the Creative Common CC-BY-SA-4.0; you may not use this file except in compliance with CC-BY-SA-4.0. You may obtain a copy of CC-BY-4.0.

Examples of SARL code are licensed under the Apache License, Version 2.0; you may not use this file except in compliance with the Apache License. You may obtain a copy of the Apache License.

You are free to reproduce the content of this page on copyleft websites such as Wikipedia.

Generated with the translator docs.generator 0.15.1.