简体   繁体   中英

Why does System.out.println have to be inside a method?

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

If I write the System.out.println inside a method,it seems to work. But not when written directly inside a class. Why is a method necessary?

It's the same as any other code that gets executed - it has to be inside a method! (Yes, ish, for the purists, I'm also including constructors and static / instance initialiser blocks.) Think about it - if it wasn't inside a method or other related code block as you propose, then when would that code get executed? It wouldn't make much sense. You can't execute a class per-se, you can only execute a specific method / constructor / etc. contained within that class.

The only things allowed outside method and constructor declarations are declarations of fields. Since System.out.println() is not a field declaration, it's not allowed.

It needs to be inside an executable block of code to be executed. Otherwise there's no way to know when to execute it.

It doesn't have to be a method. You can use other blocks, such as Static blocks and Instance blocks.

For example, if you want a code to be executed whenever the class is loaded by the ClassLoader, you can use a static block:

public class MyClass{
    static{
        System.out.println("MyClass loaded");
    }
}

If you want a code to be executed whenever a new instance of that class is created, you can use and instance block:

public class MyClass{
    {
        System.out.println("New instance of MyClass created");
    }
}

It's important to say that you can have as many of these blocks as you need, and they can appear anywhere in the class body. The Runtime system will guarantee that they are executed in the order that they appear in your class

See also:

when it would be inside a class, but outside of any method, you cannot call it, you have to create method for example getData() and calling

Employee e = new Employee().getData();

properly writes your message.

This is other way around called as initializer block. This will print "Employee class" everytime you create new Employee

class Employee {

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

}

about your question

Why does System.out.println have to be inside a method?

Java compiler is designed so to restrict such calls.
This is not only for System.out.println but is applicable for any method you call from the class.
Try calling MySystem.myout.myprintln(); //remember create such classes in advance.
Try calling your own class' method.

Don't think about a Java class as something that gets executed in order, every line is read in. Think about a Class as a fancy tool. Like

public class Bathtub {
    private int waterLevel;

    public void drain() {
        waterLevel = 0;
    }

    public void fill(int newWater) {
        waterLevel += newWater;
    }

    public void playWithDuckies() {
        System.out.println("Whee this is fun!!");
    }
}

Each method is something you can do with the Bathtub. Think about Swiss-Army-Knife, you have the scissors, the knife, the tweezers, the corkscrew. Each one does a purpose when you call it.

If you put System.out.println() outside one of these tools, you don't know when it would happen or what would make it happen or what tool it's a part of.

The System class contains several useful class fields and methods. It cannot be instantiated.

Among the facilities provided by the System class are standard input, standard output, and error output streams; access to externally defined properties and environment variables; a means of loading files and libraries; and a utility method for quickly copying a portion of an array.

out is the standard output stream and println is the method.

You can't just run code outside of a method unless it's a variable/constant declaration, a class declaration

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM