Java – why system out. Println must be inside the method?

class Employee {    
class Employee {    
    int DOB;
    int eid;
    String name;
    double salary;
    System.out.println("Employee class");
}

If I write system. In a method out. Println, it seems to work But not directly in class Why do you need a method?

Solution

It is the same as any other code executed – it must be inside the method! (yes, yes, for purists, I also include constructors and static / instance initialization blocks.) Think about it – if it's not in your proposed method or other related code block, when will the code be executed? It doesn't make much sense You cannot execute a class itself. You can only execute specific methods / constructors contained in the class

The only thing allowed by external method and constructor declarations is the declaration of fields Due to system out. Println() is not a field declaration, so it is not allowed

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>