Could you please tell me, what is the "System.
" in this code?
and why they used it?
when we should use "System.
"?
Where can I know I should use System.
for nanoTime()
?
// A class to measure time elapsed.
public class Stopwatch{
private long startTime;
private long stopTime;
public static final double NANOS_PER_SEC = 1000000000.0;
// start the stop watch.
public void start(){
startTime = System.nanoTime();
}
// stop the stop watch.
public void stop()
{ stopTime = System.nanoTime(); }
// elapsed time in seconds.
// @return the time recorded on the stopwatch in seconds
public double time()
{ return (stopTime - startTime) / NANOS_PER_SEC; }
public String toString(){
return "elapsed time: " + time() + " seconds.";
}
// elapsed time in nanoseconds.
// @return the time recorded on the stopwatch in nanoseconds
public long timeInNanoseconds()
{ return (stopTime - startTime); }
}
It's just the java.lang.System
class. (The java.lang
package is imported automatically.)
nanotime()
is a static method within System
, and out
is a static field in System
- so it's just making use of those members.
If you're not sure what static methods and fields are, you might want to read the Java tutorial.
See more on this question at Stackoverflow