This week’s puzzle is pretty simple and the question self explanatory (I hope).
package com.twisters;
public class NoConstructor {
boolean isConstructor = false; //No changes permitted to this line
/* 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
}
}
The NoConstructor class must not have any explicit constructor (that is - don’t use the word NoConstructor any more times in your solution). That's all - everything else about the puzzle should be self explanatory from the comments!
Got an answer? Leave one here
package com.twisters;
ReplyDeletepublic class NoConstructor {
boolean isConstructor = false; //No changes permitted to this line
boolean isTrue = 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
}
}
package com.twisters;
ReplyDeletepublic 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
}
}
use instance initializer
ReplyDeletepublic class NoConstructor {
ReplyDeleteboolean 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
}
}
Don't think I can get much shorter than one line...
$ javac NoConstructor.java
$ java NoConstructor
true
package com.twisters;
ReplyDeletepublic class NoConstructor {
boolean isConstructor = false; //No changes permitted to this line
{isConstructor = true;} // Instance initializer.
/* 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
}
}
The "take the comments very literally" solution:
ReplyDeletepackage com.twisters;
public class NoConstructor {
/*
boolean isConstructor = false; //No changes permitted to this line
*/
boolean 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
}
}
Just because the line can't be changed doesn't mean that you're not allowed to change the line before and after it ;)
public class NoConstructor {
ReplyDeleteboolean isConstructor = false;
{isConstructor = true;}
// or : {isConstructor ^= !isConstructor;}
public static void main(String[] args){
NoConstructor noConstructor = new NoConstructor();
System.out.println(noConstructor.isConstructor);
}
}
boolean isConstructor = false; //No changes permitted to this line
ReplyDelete{
isConstructor = true;
}
/* No Code may be added or changed in main*/
public static void main(String[] args) {
Twister noConstructor = new Twister();
System.out.println(noConstructor.isConstructor); //Prints true
}