Java Questions 31 - 40  «Prev  Next»

Static State and Main Methods in Java SE 25

Distinguish class state from instance state, implicit constructor calls, shared counters, and Java 25 instance main methods.

  1. What happens to the default constructor after you declare a constructor?

    Answer: An ordinary class gets a compiler-provided default constructor only when it declares no constructors. Declaring any constructor suppresses that default.

    A no-argument constructor may still exist if you explicitly write one. Specialized class forms, including records and enums, have additional rules; do not generalize the ordinary-class default to every class declaration.

  2. When is an implicit super() invocation supplied?

    Answer: An ordinary constructor other than Object's implicitly invokes super() when its body contains no explicit this(...) or super(...) constructor invocation. If it delegates with this, that other constructor is responsible for continuing the chain.

    The implicit no-argument call must be legal. It does not synthesize a missing parent constructor. An explicit Java 25 constructor invocation can follow a restricted prologue, but leaving it out does not delay implicit superclass construction until after the body statements.

  3. When is a static method appropriate?

    Answer: A static method belongs to a class and has no implicit receiver object. It is useful for operations naturally expressed through explicit arguments or class-level responsibilities, such as a conversion utility or factory.

    Static does not guarantee purity: the method can mutate static state or operate on objects passed as arguments. Choose it based on the API's responsibilities, not on the false assumption that a static method cannot interact with any instance.


  4. Can a static field count objects created by a class?

    Answer: Yes, if the program updates it at a clearly defined construction point. A counter incremented in a constructor measures those increments, not automatically the number of currently live objects. Delegating constructors and failed construction can complicate what is counted.

    Concurrent construction needs synchronization or an atomic counter. Static state is per class identity, including the defining loader, so it is not necessarily one global counter for every similarly named class in the JVM.

  5. What default values do static fields receive?

    Answer: They receive the same type-based defaults as instance fields: numeric zero, false for boolean, the zero code unit for char, and null for references. Static field storage is prepared as part of class linking, before ordinary explicit static initializers run.

    Constant variables have additional initialization rules. A field initializer can replace the default with the intended value; local variables do not get this automatic default initialization before use.

  6. How can a shared static counter be updated safely?

    Answer: For a simple concurrent increment, a private static final AtomicLong and incrementAndGet() provide an atomic operation. A plain count++, even on a volatile field, is a read-modify-write sequence and is not automatically atomic.

    The application still needs to define what the count means, how overflow is handled, and when it is read. Atomic increment does not make an unrelated multi-field invariant atomic. For such an invariant, a shared lock or another coordinated design may be required.


  7. Must the Java main method be static?

    Answer: No in Java SE 25. The traditional public static void main(String[] args) remains valid, but Java 25 also supports eligible instance main methods and no-argument main methods without preview flags.

    public class InstanceEntry {
        private final String greeting = "Hello from an instance";
    
        void main() {
            System.out.println(greeting);
        }
    }

    Compile InstanceEntry.java with JDK 25 and run java InstanceEntry. It prints Hello from an instance. The launcher creates the instance using the eligible no-argument construction path before invoking this instance main method.

  8. Why can't a static method make an unqualified call to an instance method of its class?

    Answer: It has no implicit this receiver. The issue is not whether objects have already been created elsewhere; even if many objects exist, the static method does not automatically know which one should receive the call.

    It can receive or create an object and call a method through that reference, such as worker.run(). Access restrictions and null checks still apply. An instance main method, unlike a static main method, has a receiver.

  9. How can you distinguish static and instance members?

    Answer: Static members belong to the declaring class; instance fields belong to individual objects and instance methods run with an object receiver. Use class-name qualification for static access and an object expression for instance access.

    A static final reference can still point to a mutable shared object. An instance method can access static members, and a static method can use explicit object references. The distinction concerns ownership and receivers, not a total separation between two worlds.

  10. What advantage does a static main entry point retain in Java 25?

    Answer: It can be invoked without constructing an instance of the initial class, preserving the familiar entry-point convention. The class still undergoes initialization before main is invoked.

    Instance main is an additional supported option. Eligible main methods return void, have either no parameters or a String array parameter, and have non-private access. When both candidate forms are available, the String-array form is preferred. Instance launch also requires a valid no-argument instance-creation path. Use the traditional form when compatibility with older Java versions or tools matters.

References: JLS: startup and initialization, JLS: static members and constructors, and AtomicLong API.


SEMrush Software