Java Tutorial/Class Definition/New
Creating Objects
- Using the new keyword.
- new is always followed by the constructor of the class.
For example, to create an Employee object, you write:
<source lang="java">
Employee employee = new Employee();</source>
When an object is created, the JVM also performs initialization that assign default values to fields.
Memory Leak Demo
<source lang="java">
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; } }
}</source>