Sunday, February 28, 2010

Java Initialization Blocks

Let's have more fun with Java. If you miss our previous post about constructors, take a look at it, here. Here is the new puzzle, what does the following code prints?
 1 package cspuzzles;
 2 
 3 class SuperClass2 {
 4     protected int x = 5;
 5 
 6     { x++;}
 7 
 8     public SuperClass2(){
 9         this(2);
10         x++;
11     }
12 
13     public SuperClass2(int i){
14         x *= i;
15     }
16 
17     { x *= 2; }
18 }
19 
20 public class Post002 extends SuperClass2{
21 
22     { x ++; }
23 
24     public Post002(){
25         x++;
26     }
27 
28     public Post002(int i){
29         super(i);
30         x *= i;
31     }
32 
33     public static void main(String[] args) {
34         Post002 p = new Post002();
35         System.out.println(p.x);
36         p = new Post002(2);
37         System.out.println(p.x);
38     }
39 } 
The tricky parts here are initialization blocks. Lines 6, 17, 22 are initialization blocks. An initialization block runs after the super constructors have run. If there is more than one initialization block, they are run in the same order as they appear in the class. Usually, initialization blocks are placed at the beginning of classes, but nothing stop us from writing one at the end of classes or in between methods.

The code prints:
27
50

The following table shows the lines executed for each of the objects created:

new Post()new Post(2)
Line 4: x = 5 Line 4: x = 5
Line 6: x = 6 Line 6: x = 6
Line 17: x = 12 Line 17: x = 12
Line 14: x = 24 Line 14: x = 24
Line 10: x = 25
Line 22: x = 26 Line 22: x = 25
Line 25: x = 27
Line 30: x = 50

Do you think initialization blocks are useful? How have you used them? Share your thoughts in the comments.

Sunday, February 21, 2010

Java Constructors

What does the following code print?
 1 package cspuzzles;
 2 
 3 class SuperSuperClass {
 4     public SuperSuperClass(){
 5         System.out.println("SuperSuperClass constructor");
 6     }
 7 
 8     public SuperSuperClass(String s){
 9         System.out.println("SuperSuperClass constructor: " + s);
10     }
11 }
12 
13 class SuperClass extends SuperSuperClass {
14     public SuperClass(){
15         System.out.println("SuperClass constructor");
16     }
17 
18     public SuperClass(String s){
19         this();
20         System.out.println("SuperClass constructor: " + s);
21     }
22 }
23 
24 public class Post001 extends SuperClass{
25     public Post001(){
26         super("Message from Post001");
27     }
28 
29     public static void main(String[] args) {
30         new Post001();
31     }
32 }
33 
It prints:
SuperSuperClass constructor
SuperClass constructor
SuperClass constructor: Message from Post001

We have a hierarchy of objects. The bottom of the hierarchy is the class Post001. Class Post001 is a subtype of SuperClass and SuperClass is a subtype of SuperSuperClass. When, a class is instantiated, for each of its superclasses at least one constructor is executed. In this example, the two constructors in SuperClass are executed but only one constructor in SuperSuperClass is executed. Let's follow the calls:
  1. Line 30: instantiates an object Post001, therefore executing the constructor of Post001.
  2. Line 26: the constructor of Post001 explicitly calls a constructor of its superclass using the keyword super. It calls the constructor version that receives a string.
  3. Line 19: the constructor calls the overloaded constructor that receives no parameters using this().
  4. Line 15: in fact, before line 15 is executed, an implicit call super() is executed resulting in a call to the no-argument constructor in SuperSuperClass.
  5. Line 5: this is the first print statement executed. To be more precise, before line 5 is executed, a call to super() is made (i.e. a call to the constructor in class Object)
  6. After this, the stack of constructor calls is returned until the program is finished.
What other tips about java constructor do you know? Let's hear about them in the comments.

See you next puzzle!

Welcome

Howdy Everybody,

Welcome to our blog. Computer Science is an amazing field and we want to share findings that we consider interesting. We will talk about inheritance, polymorphism, recursion and curious puzzles like the ones in Project Euler. We want to present each post in a puzzle-like manner, where we show first the definition of the situation and then we describe a possible solution.

We hope you give us feedback and share your thoughts in the comments, so that we can solve CS problems more efficiently and understand concepts more deeply.

See you next puzzle!