Puzzle 55 – Solution

Sunday, October 18, 2009
The solution to last time puzzle was to use an instance initializer. An instance initalizer is a piece of code similar to a static block that gets called whenever an instance is created (the compiler inlines this code at the beginning of each constructor).

package com.twisters;
public class NoConstructor {

boolean isConstructor = false; //No changes permitted to this line
{isConstructor = true;}

/* No Code may be added or changed in main*/
public static void main(String[] args) {
NoConstructor noConstructor
= new NoConstructor();
System.out.println(noConstructor.isConstructor);
//Prints true
}
}

0 comments:

Post a Comment

Solution for this question?