Method calls inside a Java class

前端 未结 8 2337
难免孤独
难免孤独 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.

提交回复
热议问题