Method calls inside a Java class

前端 未结 8 2124
难免孤独
难免孤独 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 16:23

    You dont get the error with the line

    long time = System.currentTimeMillis();
    

    because you are specifying a private variable inside the class (long time) and setting them to a default value (System.currentTimeMillis()) so when you make a new instance of the class MyClass, the variable is being instantiated.

    calling only System.currentTimeMillis() just has nosense, because you arent doing nothin' (neither having a context or assigning the value to a private variable)

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

    This is illegal. In the class body you can have only: blocks, fields, constructors, methods and classes

    Yours is neither. And what would you expect it to do anyway? If you want it to be executed when the class is instantiated, then place it in a block:

    {
        System.currentTimeMillis();
    }
    
    0 讨论(0)
提交回复
热议问题