Varargs, enumerations, and initialization blocks are three features that go beyond basic field declarations and constructors, each giving you more control over how a class and its members are configured. Varargs let a method accept any number of arguments without forcing the caller to build an array. Enumerations replace fragile magic strings or integer constants with type-safe named values — and they can carry their own data. Initialization blocks let you run setup logic before a constructor executes, with a strict and predictable order of execution that is worth understanding deeply. Together these patterns make class designs cleaner, safer, and easier to extend.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/HotCode2025/Print-Estoy-Cansado-Jefe-TercerSemestre/llms.txt
Use this file to discover all available pages before exploring further.
Varargs — Variable-Length Arguments
Normally a method that needs to receive several integers requires either multiple parameters (int a, int b, int c, ...) or an explicit array. Varargs let you declare a single Type... paramName parameter that Java automatically packs into an array inside the method body.
Syntax
- The
...notation must appear between the type and the parameter name. - Varargs must be the last parameter in the signature.
- Inside the method,
parameterNamebehaves exactly like an array.
Basic Example — imprimirNumeros
TestArgumentosVariables.java (Parte 1)
Combining Fixed and Vararg Parameters
You can mix fixed parameters with a vararg as long as the vararg comes last. The method below takes anombre and apellido as fixed String parameters and then delegates the numbers to imprimirNumeros.
TestArgumentosVariables.java (Parte 2)
Notice that
variosParametros prints "Apellido" without a colon separator — that is exactly how it appears in the source. A : is missing before the apellido value, so the output reads ApellidoMalcorra. This is a minor omission in the original code worth correcting in your own projects.| Call | numeros array inside the method |
|---|---|
imprimirNumeros(3, 4, 5) | [3, 4, 5] |
imprimirNumeros(1, 2) | [1, 2] |
variosParametros("Ernesto", "Malcorra", 7, 8, 9) | fixed params captured; numeros = [7, 8, 9] |
Enumerations (enum)
An enum is a special class type whose instances are a fixed set of named constants. Using an enum instead of raw strings or integer codes gives you compile-time type safety — the compiler rejects any value that is not one of the declared constants.
Simple Enum
The simplest form lists constants with no extra data:Dias.java
switch statement, which produces clean and readable branching logic:
TestEnumeraciones.java
Enums with Attributes — Continentes
An enum can have fields, a constructor, and methods just like a regular class. Each constant passes arguments to the constructor in parentheses. This turns an enum into a small, self-contained data object.
| Aspect | Regular class | Enum |
|---|---|---|
| Constructor visibility | Any modifier | Implicitly private — cannot be public |
| When called | Each new ClassName(...) call | Once per constant at class-load time |
| Number of instances | Unlimited | Fixed — exactly the declared constants |
Prefer enums over
public static final constants whenever the set of valid values is known and closed (days of the week, continents, HTTP methods, etc.). Enums are type-safe, support switch, and can carry behaviour — things plain constants cannot.Initialization Blocks
Java allows you to write blocks of code outside any method that run automatically when a class is loaded or when a new instance is created. These are called initialization blocks and come in two flavours.Static Initialization Block
Astatic { ... } block runs once, when the JVM first loads the class — before any constructor is called. It is the right place for setup that depends on no particular instance.
Persona.java — static block only
Because a
static block has no this reference, you cannot assign instance fields there. Attempting it causes a compile error, as the comment in the source shows.Instance (Non-Static) Initialization Block
A non-static{ ... } block (no keyword prefix) runs every time a new object is created, right before the constructor body executes.
Persona.java — static + instance block + constructor
Order of Execution
TestBloqueInicializacion.java
Execution order summary
Execution order summary
| Order | What runs | How many times |
|---|---|---|
| 1st | static { } block | Once — when the class is first loaded |
| 2nd | { } instance block | Each time a new object is created |
| 3rd | Constructor body | Each time a new object is created |
Persona objects were created. The instance block and constructor ran twice — once per object.