# AndHow Initialization

Initialization is AndHow's startup/bootstrap process where it does the following:

* Discovers its own configuration
* Discovers all declared AndHow Properties (even those in dependencies)
* Loads values for those properties from various sources using the configured Loaders
* Validates all property values

Within the lifecycle of your application, ***AndHow will initialize only once***.

### Implicit Initialization

Initialization can be triggered explicitly or implicitly.  *Implicit initialization* happens as a side effect of reading a property value:

```java
My_ANDHOW_PROPERTY.getValue();
```

The call to `Property.getValue()` forces AndHow to initialize so it can provide the value.  Later calls to getValue() of any property simply return the value loaded for that property - the initialization will only ever happen once.  This is the simplest way to initialze AndHow - just let it happen.

If your application could start up and not read any property values immediately, AndHow will not be forced to initialize and your configuration values would not be verified. In that case, you should use explicit initialization.

### Explicit Initialization

Explicit initialization happens when your application code directly constructs the AndHow instance.  We could extend the GettingStarted example to load property values from the String\[] args passed to the main method by explicitly initiating AndHow in the main method:

```java
  public static void main(String[] args) {
    AndHow.findConfig().setCmdLineArgs(args);
    
    AndHow.instance();    //  <-- Initialize

    Thread.sleep(1000000);
    checkForTaskToDo();    //  <-- Configuration is first read here
  }
```

In this hypothetical example, configuration values are not used until there is a task to do, perhaps long after the process has started.  To ensure configuration values are validated at startup, use `AndHow.instance()` to force initialization of the AndHow singleton.  This ensures that any mis-configuration [*fails fast*](https://www.martinfowler.com/ieeeSoftware/failFast.pdf).

Later calls to `AndHow.instance()` will return the single `AndHow` instance.  Later calls to `Property.getValue()` will internally call `AndHow.instance()` to look up the property value.

In the example above, the call to `AndHow.findConfig()` does not cause AndHow to initialize.  Instead, it retrieves the configuration that AndHow will use during initialization.  Calling `AndHow.findConfig()` is only allowed ***before AndHow is initialized.***

### Attempting to reconfigure AndHow will throw a RuntimeException

Here is another initialization example:

```java
  public class LambdaHandler {
    public void prepare() {
      System.out.println(A_PROP.getValue());
    }
    
    public String handle(String request) {
      AndHow.findConfig().addFixedValue(MY_ANDHOW_PROP, "xxx");
    }
  }
```

In the code snippets above, it looks like the intent is to set a fixed value of "xxx" for `A_PROP`, but perhaps `prepare()` is called before that happens!?  In that case, `A_PROP` might have the 'wrong' value.

AndHow protects against this situation by blocking access to `AndHow.findConfig()` after initialization happens.  If `prepare()` is called before `handle()`, implicit initialization happens at `A_PROP.getValue()`.  Later when `AndHow.findConfig()` is called, AndHow throws a RuntimeException.

{% hint style="info" %}

#### Best Practice:  If your application needs to configure AndHow, use an `AndHowInit` class&#x20;

Alternatively, ensure the application has a well defined application entry point.

See [Configuring AndHow](/user-guide/configuring-andhow.md) for more details.
{% endhint %}

, which is always discovered and invoked during initialization, even if implicitly initiated.&#x20;

## AndHow Initialization Steps in Detail

AndHow will use the [StdConfig](https://www.google.com/url?q=https%3A%2F%2Fgithub.com%2Feeverman%2Fandhow%2Fblob%2Fmaster%2Fandhow-core%2Fsrc%2Fmain%2Fjava%2Forg%2Fyarnandtail%2Fandhow%2FStdConfig.java\&sa=D\&sntz=1\&usg=AFQjCNEC40ZsNv0lRJNfJ0Smz0jnzI4OwA) to configure itself, which is an instance of [AndHowConfiguration](https://www.google.com/url?q=https%3A%2F%2Fgithub.com%2Feeverman%2Fandhow%2Fblob%2Fmaster%2Fandhow-core%2Fsrc%2Fmain%2Fjava%2Forg%2Fyarnandtail%2Fandhow%2FAndHowConfiguration.java\&sa=D\&sntz=1\&usg=AFQjCNE0x6ZDGcLu1T3CqgyEg2hnneR9CA), unless there is an implementation of [AndHowInit](https://www.google.com/url?q=https%3A%2F%2Fgithub.com%2Feeverman%2Fandhow%2Fblob%2Fmaster%2Fandhow-core%2Fsrc%2Fmain%2Fjava%2Forg%2Fyarnandtail%2Fandhow%2FAndHowInit.java\&sa=D\&sntz=1\&usg=AFQjCNF8s1hLBuP-Bu030Y8f47v49e17xw) is on the classpath. AndHowInit is an interface with a single method: getConfiguration() which returns AndHowConfiguration. Common initiation needs, like injecting *String\[] args* or adding fixed values can be handled in-line with explicit initiation (example above). For more detailed control, subclassing StdConfig and providing an AndHowInit implementation is needed.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://www.andhowconfig.org/user-guide/andhow-initialization.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
