Skip to main content

static

The static keyword in Soul is used to declare methods and properties that belong to the class itself rather than to instances of the class. Static members can be accessed without creating an instance of the class.

Basic Static Methods

Define static methods using the static keyword:

Static vs Instance Methods

Compare static and instance methods:

Static Utility Methods

Use static methods for utility functions:

Static Factory Methods

Use static methods as factory constructors:

Static Configuration

Use static methods for configuration:

Static Validation Methods

Use static methods for validation:

Static Constants

Use static methods to provide constants:

Static Helper Methods

Use static methods for helper functions:

Static Method Chaining

Chain static methods for fluent interfaces:

Static Error Handling

Use static methods for error handling:

Static Access Control

Control access to static methods:

Best Practices

  1. Use static for utilities: Methods that don’t need instance state
  2. Use static for factories: Alternative constructors and object creation
  3. Use static for constants: Configuration and constant values
  4. Keep static methods pure: Avoid side effects when possible
  5. Group related static methods: Organize static methods in logical classes
Static methods are powerful tools for creating utility functions, factory methods, and class-level functionality that doesn’t depend on instance state. Use them to organize related functionality and create clean, reusable code.