CSCI 217: Advanced Programming Lab 02 1 Constructor • Same as method. • Called when an instance of class is created. • Used to initialize the object. • Its name should be same as class name. 2 UML Diagram 3 UML Class Diagram 4 This Keyword • Reference variable • Refer to the current object class Test { int a; int b; Test(int a, int b) { this.a = a; this.b = b; } a=10 b=20 void display() { System.out.println("a = " + a + " } b = " + b); public static void main(String[] args) { Test object = new Test(10, 20); object.display(); } } 5 class Test { int a; int b; Test() { a = 10; b = 20; } a=10 b=20 Test get() { return this; } void display() { System.out.println("a = " + a + " } b = " + b); public static void main(String[] args) { Test object = new Test(); object.get().display(); } } 6 Static • • • • Static Blocks Static Methods Static Class Static Variable 7 Task1.1 trace: 8 Task1.2 trace: Static Method 9 Task2: 10