search

Home  >  Q&A  >  body text

7-24 Why can the constructor be directly in the class and output directly to the console?

class Student() {public Student (){System.out.println("This is the constructor")}}

class ConstructDemo{public static void main(String[] args){Student s =new Student();

System.out.println(s);

}} Why "This is the construction method" can be output to the console without being called in the main method

wsws788 days ago830

reply all(1)I'll reply

  • WBOY

    WBOY2022-09-21 17:57:14

    Because the general entry point of a Java program is the main function, execution starts from the main function. A student object is instantiated in the main function. When an object is instantiated, the constructor of this class will be called. The parameterless constructor of this class is defined in the student class, and there are output statements in the method.

    reply
    0
  • Cancelreply