Q:

variable initialization of a class

I have a doubt regarding the initialization of variables of a class.
When usually declare a class with the variables and I initialize (even if they are final) I do it via the constructor.
For example:
class Example {
    private int a, b;
   
    public Example () {
        a = 5;
        b = 10;
    }

    // Methods
}

But you can also initiate immediately after the statement.
For example:
class Example {
    private int a = 5, b = 10;
   
    // Methods
}

What is the best way to initialize variables? What is the difference?

A:

The variables initialized the first way will get their values when the init<> method is run.   The variable initialized in the second way will get their value when the constructor is executed.   For simple use cases like this there is no effective difference, but if you have multiple constructors or "interesting" things happening with the object lifecycle, it can matter.   There is no always-better way.
(One example of how you'd get different behavior is if a superclass' constructor - which must run before the subclass - used reflection to act based on the instance field variables somehow.  This isn't a very common thing to need to happen, but it's an example of what I meant by "interesting"...)

