Home  >  Q&A  >  body text

In Java, can System.out.println be used only in method?

class test_1{
    test_1(){
        System.out.println("jijiji");
    }
    System.out.println("jijiji");
}

public class Main {

    public static void main(String[] args) {
        test_1 test = new test_1();
    }
}

In the test_1 class, the print statement in the constructor test_1() did not report an error, but its next line reported an error and showed that an identifier is required.
Could you please explain this What is the reason? Java's System.out.println()must be used in method?

PHP中文网PHP中文网2702 days ago592

reply all(1)I'll reply

  • 给我你的怀抱

    给我你的怀抱2017-05-27 17:43:48

    This class itself is composed of methods and objects. What you print is actually the method body, so it must be placed in the method

    reply
    0
  • Cancelreply