What is the smallest change to main method that makes it sysout “true”?
You are allowed to change ONLY the main method! Of course changing sysout line is out of question.
You can leave your answer in comments and I’ll publish/approve them together with the solution.
[java]
public class InstanceDemo {
public static void main(String[] args){
MyClass cl1 = new MyClass();
MyClass cl2 = new MyClass();
System.out.println(cl1.ocl==cl2.ocl && cl1!=cl2);
}
private static class MyClass extends java.util.ArrayList {
private final Object ocl = new Object();
}
}
[/java]
I’ll post the correct answer in 24h
UPDATE: Was not that hard afterall!
~45 correct answers.
Yes, clone() was the answer I was looking for:
[java]MyClass cl2 = (MyClass) cl1.clone();[/java]
Ed Merks surprised with a nice solution:
[java]
//Add this line as the first line of main.
class MyClass extends InstanceDemo.MyClass {
Object ocl = null;
}
[/java]
And a lot of people took the hard-core way:
[java]
Field field = cl1.getClass().getDeclaredField(“ocl”);
field.setAccessible(true);
field.set(cl2,cl1.ocl);
[/java]


RSS
LinkedIn
Facebook
49 Comments
The most obvious answer would be:
- MyClass cl2 = new MyClass();
+ MyClass cl2 = cl1.clone();
My 0.02 are on “cl2=(MyClass)cl1.clone();” right before the println.
dominikg
is it the good answer ?
MyClass cl2 = (MyClass) cl1.clone();
MyClass cl2 = (MyClass) cl1.clone();
Gilles QUERRET
change line 2 in main to
MyClass cl2 = (MyClass)cl1.clone();
public static void main(String[] args)
{
final MyClass cl1 = new MyClass();
MyClass cl2 = new MyClass()
{
{
try
{
Field firstNameField = MyClass.class.getDeclaredField(“ocl”);
firstNameField.setAccessible(true);
firstNameField.set(this, cl1.ocl);
}
catch (Exception e)
{
// ignore
}
}
};
System.out.println(cl1.ocl == cl2.ocl && cl1 != cl2);
}
From the master hackers of dow.ngra.de:
——————————————
public static void main(String[] args) throws Exception {
class MyClass { Object ocl = null;}
MyClass cl1 = new MyClass();
MyClass cl2 = new MyClass();
System.out.println(cl1.ocl == cl2.ocl && cl1 != cl2);
}
——————————————
My first guess was to break into MyClass with reflection and to change the ocl attribute. But it actually is much easier: Instead of
MyClass cl2 = new MyClass();Say
MyClass cl2 = (MyClass) cl1.clone();I was sure that the “extends ArrayList” was there for a reason, it just took me a moment to figure it out.
Nice.
Add this line as the first line of main.
class MyClass extends InstanceDemo.MyClass {
Object ocl = null;
}
Hi here is an answer :
MyClass cl1 = new MyClass();
MyClass cl2 = (MyClass) cl1.clone();
System.out.println(cl1.ocl == cl2.ocl && cl1 != cl2);
Is there any practical case where this can be useful ?
Manu
Make cl2 a clone of cl1:
MyClass cl2 = (MyClass)cl1.clone();
package de.tutorials;
public class InstanceDemo {
public static void main(String[] args) {
MyClass cl1 = new MyClass();
//MyClass cl2 = new MyClass();
MyClass cl2 = (MyClass)cl1.clone();
System.out.println(cl1.ocl == cl2.ocl && cl1 != cl2);
}
private static class MyClass extends java.util.ArrayList {
private final Object ocl = new Object();
}
}
public class InstanceDemo {public static void main(String[] args){
MyClass cl1 = new MyClass();
MyClass cl2 = (MyClass) cl1.clone();
System.out.println(cl1.ocl==cl2.ocl && cl1!=cl2);
}
private static class MyClass extends java.util.ArrayList {
private final Object ocl = new Object();
}
}
Nice touch on extends ArrayList!
Robert
MyClass cl2 = (MyClass) cl1.clone();
public class InstanceDemo {
public static void main(String[] args){
MyClass cl1 = new MyClass();
MyClass cl2 = (MyClass) cl1.clone();
System.out.println(cl1.ocl==cl2.ocl && cl1!=cl2);
}
private static class MyClass extends java.util.ArrayList {
private final Object ocl = new Object();
}
}
Simplest I could think of…
public class InstanceDemo
{
public static void main(String[] args)
{
MyClass cl1 = new MyClass();
MyClass cl2 = (MyClass)cl1.clone();
System.out.println(cl1.ocl == cl2.ocl && cl1 != cl2);
}
private static class MyClass extends java.util.ArrayList
{
private final Object ocl = new Object();
}
}
How about this:
public static void main(String[] args) throws Exception
{
MyClass cl1 = new MyClass();
MyClass cl2 = new MyClass();
Field f = MyClass.class.getDeclaredField(“ocl”);
f.setAccessible(true);
f.set(cl2, cl1.ocl);
System.out.println(cl1.ocl == cl2.ocl && cl1 != cl2);
}
Regards,
Johan
MyClass cl2 = (MyClass)cl1.clone();
public class InstanceDemo {
public static void main(String[] args){
MyClass cl1 = new MyClass();
MyClass cl2 = (MyClass) cl1.clone();
System.out.println(cl1.ocl==cl2.ocl && cl1!=cl2);
}
private static class MyClass extends java.util.ArrayList {
private final Object ocl = new Object();
}
}
MyClass cl2 = (MyClass) cl1.clone();
Off the top of my head (it’s been a while I haven’t done some java)
Replace
MyClass cl2 = new MyClass();
by
MyClass cl2 = (MyClass)cl1.clone();
public static void main(String[] args){
MyClass cl1 = new MyClass();
MyClass cl2 = (MyClass)cl1.clone();
System.out.println(cl1.ocl==cl2.ocl && cl1!=cl2);
}
// In diff format.
- MyClass cl2 = new MyClass();
+ MyClass cl2 = (MyClass) cl1.clone();
One more reason why I try to avoid the clone method.
MyClass cl1 = new MyClass();
MyClass cl2 = (MyClass)cl1.clone();
public class InstanceDemo {
public static void main(String[] args){
MyClass cl1 = new MyClass();
MyClass cl2 = new MyClass();
System.out.println(cl1.ocl!=cl2.ocl && cl1!=cl2);
}
private static class MyClass extends java.util.ArrayList {
private final Object ocl = new Object();
}
}
I changed 1 character, but I bet this is not what you expected
Oops, I changed the sysout line. So much to play the smart guy
Something like MyClass cl2 = cl1.clone();
Change line 4 to:
MyClass cl2 = (MyClass)cl1.clone();
Is there a smaller change? I can see no other way without editing MyClass…
How about
public class InstanceDemo {
public static void main(String[] args){
MyClass cl1 = new MyClass();
MyClass cl2 = (MyClass) cl1.clone();
System.out.println(cl1.ocl==cl2.ocl && cl1!=cl2);
}
private static class MyClass extends java.util.ArrayList {
private final Object ocl = new Object();
}
}
??
Cheers,
Daniel
…
private final Object ocl = “aString”;
…
MyClass cl2 = (MyClass) cl1.clone();
public class InstanceDemo {
public static void main(String[] args) {
MyClass cl1 = new MyClass();
MyClass cl2 = (MyClass) cl1.clone();
System.out.println(cl1.ocl == cl2.ocl && cl1 != cl2);
}
private static class MyClass extends java.util.ArrayList {
private final Object ocl = new Object();
}
}
I would change the line that reads, “MyClass cl2 = new MyClass();” to be MyClass cl2 = (MyClass) cl1.clone();”
public class InstanceDemo {
public static void main(String[] args) {
MyClass cl1 = new MyClass();
MyClass cl2 = (MyClass) cl1.clone();
System.out.println(cl1.ocl == cl2.ocl && cl1 != cl2);
}
private static class MyClass extends java.util.ArrayList {
private final Object ocl = new Object();
}
}
Adding a “throws Exception” and
Field field = cl1.getClass().getDeclaredField( "ocl" );
field.setAccessible( true );
field.set( cl2, cl1.ocl );
works, unless you have a restrictive SecurityManager, but is certainly no small change and very ugly.
Smaller but still rather mad and ugly is using a method private class, by adding this as the first line in main:
class MyClass{Object ocl;}
I just learned about this very recently on stackoverflow…
Change:
MyClass cl2 = new MyClass();
to:
MyClass cl2 = (MyClass)cl1.clone();
MyClass cl2 = (MyClass) cl1.clone();
My solution is using reflection, but I suppose this is not what you mean by “the correct answer”.
public static void main(String[] args) throws Exception {
MyClass cl1 = new MyClass();
MyClass cl2 = new MyClass();
Field ocl = MyClass.class.getDeclaredField("ocl");
ocl.setAccessible(true);
ocl.set(cl1, null);
ocl.set(cl2, null);
System.out.println(cl1.ocl == cl2.ocl && cl1 != cl2);
}
Replace the 4th non-blank line with this:
MyClass cl2 = (MyClass)cl1.clone();
Do I win???
public class InstanceDemo {
public static void main(String[] args) {
MyClass cl1 = new MyClass();
MyClass cl2 = (MyClass)(cl1.clone());
System.out.println(cl1.ocl == cl2.ocl && cl1 != cl2);
}
private static class MyClass extends java.util.ArrayList {
private final Object ocl = new Object();
}
}
Changing to
MyClass cl2 = (MyClass)cl1.clone();
Should make a trick. Can you publish the statistics about this question? Just wondering.
Thanks,
Alex.
Change “MyClass cl2 = new MyClass();” to:
MyClass cl2 = (MyClass)cl1.clone();
public static void main(String[] args) throws Exception {
MyClass cl1 = new MyClass();
MyClass cl2 = new MyClass();
Field field = cl1.getClass().getDeclaredField("ocl");
field.setAccessible(true);
field.set(cl2,cl1.ocl);
System.out.println(cl1.ocl == cl2.ocl && cl1 != cl2);
}
MyClass cl2 = (MyClass) cl1.clone();
public class InstanceDemo {
public static void main(final String[] args) {
MyClass cl1 = new MyClass();
MyClass cl2 = (MyClass)cl1.clone();
System.out.println(cl1.ocl == cl2.ocl && cl1 != cl2);
}
private static class MyClass extends java.util.ArrayList {
private final Object ocl = new Object();
}
}
private static final Object ocl = new Object();
not that hard compared to some SCJP questions
I’ll give it a try…
public class InstanceDemo {
public static void main(String[] args){
MyClass cl1 = new MyClass();
MyClass cl2 = (MyClass) cl1.clone();
System.out.println(cl1.ocl==cl2.ocl && cl1!=cl2);
}
private static class MyClass extends java.util.ArrayList {
private final Object ocl = new Object();
}
}
shamaz,
“private static final Object ocl = new Object();”
is not correct because you are allowed to change ONLY main method.
The idea of using a method private class is sweet and can be shortend by a few more characters, if you use an int instead of an object:
class MyClass {int ocl;}in the beginning of main() does the trick.