Method calls inside a Java class

前端 未结 8 2123
难免孤独
难免孤独 2020-11-28 15:31

I was setting out to write a piece of code today in Eclipse, and I started out like so:

public class MyClass {
    System.currentTimeMillis();
}


        
相关标签:
8条回答
  • 2020-11-28 15:59

    Is there some compiler level rule that says that only assignment statements or declarations should be present inside the class body?

    In a word: yes. At the class body level you can have instance and static member variable declarations, method declarations, nested classes, object initialization blocks, static initialization blocks, and comments. That's it, by definition.

    The "compiler level rules" for a language are called its grammar.

    0 讨论(0)
  • 2020-11-28 16:00

    The class body can only contain declarations.

    Specifically, § 8.1.6 of the JLS defines the class body like this:

    A class body may contain declarations of members of the class, that is, fields (§8.3), classes (§8.5), interfaces (§8.5) and methods (§8.4). A class body may also contain instance initializers (§8.6), static initializers (§8.7), and declarations of constructors (§8.8) for the class.

        ClassBody:
          { ClassBodyDeclarationsopt }
    ClassBodyDeclarations: ClassBodyDeclaration ClassBodyDeclarations ClassBodyDeclaration
    ClassBodyDeclaration: ClassMemberDeclaration InstanceInitializer StaticInitializer ConstructorDeclaration
    ClassMemberDeclaration: FieldDeclaration MethodDeclaration ClassDeclaration InterfaceDeclaration ;

    As you can see, there are no statements in there anyway, so a class body may not directly contain a statement.

    If you think about it, it makes sense: at which point should that code be executed? There is no context to tell you about that, so it makes no sense.

    0 讨论(0)
  • 2020-11-28 16:02
    long time = System.currentTimeMillis();
    

    is an instance variable, which gets initalized to the current time when the enclosing Object is created. Valid.

    System.currentTimeMillis();
    

    is a statement on its own. Invalid, outwith a constructor, method, static iniatilizer etc.

    0 讨论(0)
  • 2020-11-28 16:07

    Is there some compiler level rule that says that only assignment statements or declarations should be present inside the class body?

    Yes. More specifically, the syntactic rules of programming languages are usually defined as a formal grammar that specifies how syntactically correct programs are formed. In this case, the Java language specification says:

    ClassBody:
        { ClassBodyDeclarationsopt }
    
    ClassBodyDeclarations:
        ClassBodyDeclaration
        ClassBodyDeclarations ClassBodyDeclaration
    
    ClassBodyDeclaration:
        ClassMemberDeclaration
        InstanceInitializer
        StaticInitializer
        ConstructorDeclaration
    

    Since a static method call is not one of ClassMemberDeclaration, InstanceInitializer, StaticInitializer and ConstructorDeclaration, it's not allowed to be present directly inside a class body.

    0 讨论(0)
  • 2020-11-28 16:16

    You need to call your code from inside a method, not just on its own like that. E.g.

    public class MyClass {
        public static void main(String[] args)
        {
            System.currentTimeMillis();
        }
    }
    

    The above still won't do anything, but it's legal :-)

    0 讨论(0)
  • 2020-11-28 16:20

    Try this way:

    public class MyClass {
        static {
            System.currentTimeMillis();
        }
    }
    

    If you call System.currentTimeMillis() inside a static statement it works. The static block will be called when the class "MyClass" is loaded by the class loader.

    0 讨论(0)
提交回复
热议问题