Actual use of default constructor in java

Default constructors are provided by the compiler when the programmer fails to write any constructor to a class. And it is said that these constructors are used to initialize default values to the class attributes.However, if the programmer provides a constructor, be it a simple one like:

public class Main {
  int a;

  Main() { // user defined simple constructor
    System.out.println("hello");
  }

  public static main(String[] args) {
    Main obj = new Main();
  }
}

In the above code, the user has included a constructor. However, it doesn’t initialize the instance variable(a). Moreover, the default constructor won’t be called. Then how come the variable ‘a’ gets initialized to its default value.

If it is like, the default constructors do not initialize the class variables to their default values and the compiler does it automatically, then what is the actual use of the default constructor?

Why does the compiler add a default constructor in the case of java vs Kotlin when the user fails to write a constructor?

That’s not a problem, ints get assigned a value of 0.

There’s not default constructor in this case, since the user provided one.

Java doesn’t really have uninitalized values, objects are null, numbers are zero, booleans are false (I think)

I’m not sure what does this mean, the fact that the compier generates a default constructor is just to save 2 lines of code to write Main() {}, that’s it.

They show up as no-argument constructors in reflection, and you can get method references to them and pass a default constructor like Main::new to a method that takes a Supplier<Main>.

Also, I believe a lot of other language in the spec would imply that you couldn’t construct an instance of a class with no constructors, so in that way the default constructor is what allows you to create instances when a constructor isn’t declared.

In the above code, the user has included a constructor. However, it doesn’t initialize the instance variable(a). Moreover, the default constructor won’t be called. Then how come the variable ‘a’ gets initialized to its default value.

The default constructor looks like this:

 Main() {}

The compiler initializes variables at the beginning of every constructor, including this one. It does the same thing whether you write it or get it by default.