The example illustrates various features of the class System
// the program illustrates using the System class
import java.io.*; // since we are playing with input/output
public class SystemTest {
public static void main(String [] args) throws IOException {
MyObject mo = new MyObject(5);
if (2 < 3) {
MyObject mo1 = new MyObject(2);
}
aMethod();
// calling the garbage-collector:
// System.gc();
// Other system methods:
String javaVersion = System.getProperty("java.version");
System.out.println(javaVersion);
String username = System.getProperty("user.name");
System.out.println(username);
String osname = System.getProperty("os.name");
System.out.println(osname);
// Setting the output
System.setOut(new PrintStream(new FileOutputStream("new.txt")));
System.out.println("Hi there!");
}
private static void aMethod() {
MyObject mo = new MyObject(3);
System.out.println(mo.toString());
}
}
class MyObject {
private int n;
public MyObject(int x) {
n = x;
}
public String toString() {
return "MyObject: " + n;
}
protected void finalize() {
System.out.println(toString() + " is being garbage-collected");
}
}
This is an example from CSci 1211 course.