Inner Classes
Named classes defined inside other classes for encapsulation and organization
Inner classes are named classes defined inside the body of another class in a .bx file. They let you organize related helper classes alongside their parent, keeping implementation details encapsulated while still being accessible when needed.
If template classes let you define helpers inline in scripts, inner classes let you do the same thing inside your classes themselves.
class Outer {
class Inner {
function getValue() {
return "inner";
}
}
function getInner() {
return new Inner();
}
}
result = new Outer().getInner().getValue();
// Result: "inner"π₯ Defining Inner Classes
An inner class is declared using the class keyword with a name, inside the body of an outer class:
Inner classes can have all the same features as top-level classes:
Properties
Constructors (
init())Functions
Static blocks
Annotations
extendsandimplements
π Instantiating Inner Classes
Inner classes are instantiated using new from within the outer class:
Inner class instances are IClassRunnable objects, just like top-level class instances.
π¦ Multiple Inner Classes
A single outer class can contain multiple inner classes:
πͺ Nested Inner Classes
Inner classes can themselves contain inner classes, creating multiple levels of nesting:
π Inheritance
Inner classes can extend other inner classes defined in the same outer class:
π§ Static Members
Inner classes can have static blocks and static members:
π Accessing Outer Class Statics
Inner classes can access the outer class's static members using both dot notation and double-colon syntax:
The outer class can also reference its own statics by name:
ποΈ Hoisting
Inner classes are hoisted within the class body, meaning you can reference and instantiate an inner class before its definition:
π External Access
Inner classes are compiled as sibling JVM classes with $-delimited names (e.g., OuterClass$Inner). This means they can be accessed from outside the outer class using the $ separator syntax.
Instantiating via $ Syntax
Accessing Statics via $ Syntax
Nested Inner Classes via Chained $
π₯ Importing Inner Classes
Inner classes can be imported using the $ separator syntax:
Importing with Alias
Accessing Statics After Import
Referencing Inner Classes via Outer Class
You can also reference inner classes statically via the outer class name using dot or double-colon syntax:
Using createObject()
π Metadata
Inner classes expose metadata through $bx (ClassMeta) and getMetadata().
$bx Metadata
getMetadata() Struct
For an inner class instance:
getClassMetadata()
isInstanceOf() with Inner Classes
β Java Interoperability
Inner classes can implement Java interfaces, making them useful for Java interop patterns:
β οΈ Naming Restrictions
Outer Class Name is Reserved
When a class has inner classes, the outer class name becomes a reserved variable name. You cannot assign to it or use it as a function argument:
Inner Class Cannot Match File Name
An inner class cannot have the same name as the enclosing file's class name:
Inner Class Names Cannot Be Assigned as Statics
You cannot overwrite an inner class name via static assignment:
Last updated
Was this helpful?
