Java Tutorial/Class Definition/New

Материал из Java эксперт
Перейти к: навигация, поиск

Creating Objects

  1. Using the new keyword.
  2. new is always followed by the constructor of the class.

For example, to create an Employee object, you write:



Employee employee = new Employee();



When an object is created, the JVM also performs initialization that assign default values to fields.


Memory Leak Demo

class List {
  MemoryLeak mem;
  List next;
}
class MemoryLeak {
  static List top;
  char[] memory = new char[100000];
  public static void main(String[] args) {
    for (int i = 0; i < 100000; i++) {
      List temp = new List();
      temp.mem = new MemoryLeak();
      temp.next = top;
      top = temp;
    }
  }
}