invariant encapsulation life cycle of an object 1. created - on the heap 2. (fields cannot be modified) methods are invoked 3. destroyed? class A { ... .... } A aobj = new A(); // 1. 1000 6246 1. 2. aobj --| |----| |---|1000|---------------- \-----/ aobj = new A(); // 2. 1000 6246 1. 2. aobj --| |----| |---|6246|---------------- aobj = null; A aobj = new A(); // 1. A bobj = aobj; aobj = new A(); // 2. A aobj = new A(); // 1. aobj = new A(); // 2. not Java: pointers Java: references garbage collection (new A()).f(); list.add(new A()); aobj val text dval1 dval2 1234 6789 8888 ---|5 |8888|1.457|6789|---|"foo"|---|1.457|----|"bar"|----------------- A aobj = new A(); aobj.val = 5; aobj.text = "foo"; aobj.dval1 = 1.457; aobj.dval2 = new Double(1.457); aobj.text = "bar"; int[] ia1 = new int[3]; Integer[] ia2 = new Integer[3]; ia1 addr1 ia2 addr3 addr2 -|1|2|3|---|1|---|addr1|addr2|addr3|-----|3|------|2|----------------------------------- class A { A() { val = 123; text = "asd"; dval1 = 1.2; dval2 = -523.335; } A(int i) {} A(int i, String t, double d) {} A(int v, String t, double d, Double d2) { this.val = v; this.text = t; this.dval1 = d; this.dval2 = d2; } A f() { int localVar; return this; } int val; String text; double dval1; Double dval2; } Python: def f(self, .........) class B { // if no constructor inside: // public B() {} int field1; } C++ ---|-56|5325|253.3|23.5|--- 5325 --------------------------------------------|"gdfsdfgfasd"|--------- class Person { int age = 1; } class Person { int age; Person() { age = 4; } } class Person { int age; // automatically: 0 boolean b; // automatically: false String s; // automatically: null Person() {} } class C { // f is overloaded void f() {} // void f(int a) {} void f(double d) {} } C c = new C(); c.f(); c.f(1); // automatic type conversion c.f((double)1); // explicit type conversion c.f(1.0); c.f(new X()); // not working class C { // f is overloaded void f(int a, double d) {} void f(double d, int a) {} void f(double d, double d2) {} } C c = new C(); c.f(1, 1.0); c.f(1.0, 1); c.f(1.0, 1.0); c.f(1, 1); class C { // f is overloaded void f(int a, double d) {} void f(double d, double d2) {} } C c = new C(); c.f(1, 1); class C { void f1(int a, double d) {} void f2(double d, int a) {} void f3(double d, double d2) {} } class Name { // fields // constructors // methods }