Java Quiz of The Day – same private field instance for two classes

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] :)

49 Comments

  1. The most obvious answer would be:

    - MyClass cl2 = new MyClass();
    + MyClass cl2 = cl1.clone();

    Roland Tepp
    Posted December 1, 2008 at 12:43 pm | Permalink
  2. My 0.02 are on “cl2=(MyClass)cl1.clone();” right before the println.

    dominikg

    dominikg
    Posted December 1, 2008 at 12:43 pm | Permalink
  3. is it the good answer ?
    MyClass cl2 = (MyClass) cl1.clone();

    Tristan
    Posted December 1, 2008 at 12:45 pm | Permalink
  4. MyClass cl2 = (MyClass) cl1.clone();

    Gilles QUERRET

    Posted December 1, 2008 at 12:57 pm | Permalink
  5. change line 2 in main to

    MyClass cl2 = (MyClass)cl1.clone();

    fillg1
    Posted December 1, 2008 at 1:12 pm | Permalink
  6. 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);
    }

    Sergiy
    Posted December 1, 2008 at 1:16 pm | Permalink
  7. 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);
    }
    ——————————————

    Posted December 1, 2008 at 1:26 pm | Permalink
  8. 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.

    Posted December 1, 2008 at 1:33 pm | Permalink
  9. Add this line as the first line of main.

    class MyClass extends InstanceDemo.MyClass {
    Object ocl = null;
    }

    Posted December 1, 2008 at 1:35 pm | Permalink
  10. 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

    Posted December 1, 2008 at 2:19 pm | Permalink
  11. Make cl2 a clone of cl1:

    MyClass cl2 = (MyClass)cl1.clone();

    Ancuta
    Posted December 1, 2008 at 2:21 pm | Permalink
  12. 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();
    }

    }

    Posted December 1, 2008 at 2:51 pm | Permalink
  13. 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

    Robert Munteanu
    Posted December 1, 2008 at 3:44 pm | Permalink
  14. MyClass cl2 = (MyClass) cl1.clone();

    DrMastaP
    Posted December 1, 2008 at 3:46 pm | Permalink

  15. 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…

    koala
    Posted December 1, 2008 at 3:54 pm | Permalink
  16. 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();
    }

    }

    Daniel
    Posted December 1, 2008 at 4:00 pm | Permalink
  17. 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

    Johan
    Posted December 1, 2008 at 4:24 pm | Permalink
  18. MyClass cl2 = (MyClass)cl1.clone();

    kmast
    Posted December 1, 2008 at 4:29 pm | Permalink
  19. 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();
    }

    }

    Yuriy
    Posted December 1, 2008 at 4:41 pm | Permalink
  20. MyClass cl2 = (MyClass) cl1.clone();

    Posted December 1, 2008 at 5:00 pm | Permalink
  21. 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();

    Posted December 1, 2008 at 5:11 pm | Permalink

  22. public static void main(String[] args){
    MyClass cl1 = new MyClass();
    MyClass cl2 = (MyClass)cl1.clone();
    System.out.println(cl1.ocl==cl2.ocl && cl1!=cl2);
    }

    Posted December 1, 2008 at 5:29 pm | Permalink
  23. // In diff format.
    - MyClass cl2 = new MyClass();
    + MyClass cl2 = (MyClass) cl1.clone();

    Joakim E
    Posted December 1, 2008 at 5:38 pm | Permalink
  24. One more reason why I try to avoid the clone method.

    MyClass cl1 = new MyClass();
    MyClass cl2 = (MyClass)cl1.clone();

    Cole
    Posted December 1, 2008 at 6:00 pm | Permalink

  25. 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 :-)

    Jerome Lanneluc
    Posted December 1, 2008 at 6:01 pm | Permalink
  26. Oops, I changed the sysout line. So much to play the smart guy :-)

    Jerome Lanneluc
    Posted December 1, 2008 at 6:03 pm | Permalink
  27. Something like MyClass cl2 = cl1.clone();

    Jean Palies
    Posted December 1, 2008 at 6:08 pm | Permalink
  28. Change line 4 to:
    MyClass cl2 = (MyClass)cl1.clone();
    Is there a smaller change? I can see no other way without editing MyClass

    Holger
    Posted December 1, 2008 at 6:18 pm | Permalink
  29. 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

    Daniel
    Posted December 1, 2008 at 6:54 pm | Permalink

  30. private final Object ocl = “aString”;

    Posted December 1, 2008 at 7:12 pm | Permalink
  31. MyClass cl2 = (MyClass) cl1.clone();

    Phillipus
    Posted December 1, 2008 at 7:53 pm | Permalink

  32. 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();
    }

    }

    Posted December 1, 2008 at 8:58 pm | Permalink
  33. I would change the line that reads, “MyClass cl2 = new MyClass();” to be MyClass cl2 = (MyClass) cl1.clone();”

    Chris
    Posted December 1, 2008 at 9:12 pm | Permalink
  34. 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();
    }

    }

    Kiril Mitov
    Posted December 1, 2008 at 10:18 pm | Permalink
  35. 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…

    Michael
    Posted December 1, 2008 at 10:32 pm | Permalink
  36. Change:
    MyClass cl2 = new MyClass();
    to:
    MyClass cl2 = (MyClass)cl1.clone();

    H
    Posted December 1, 2008 at 10:57 pm | Permalink
  37. MyClass cl2 = (MyClass) cl1.clone();

    Posted December 1, 2008 at 11:46 pm | Permalink
  38. 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);
    }

    Radoslaw Jozwik
    Posted December 2, 2008 at 12:09 am | Permalink
  39. Replace the 4th non-blank line with this:

    MyClass cl2 = (MyClass)cl1.clone();

    Do I win???

    Dave Weatherford
    Posted December 2, 2008 at 1:21 am | Permalink
  40. 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();
    }

    }

    Posted December 2, 2008 at 1:52 am | Permalink
  41. Changing to

    MyClass cl2 = (MyClass)cl1.clone();

    Should make a trick. Can you publish the statistics about this question? Just wondering.

    Thanks,
    Alex.

    Alex Smirnoff
    Posted December 2, 2008 at 2:24 am | Permalink
  42. Change “MyClass cl2 = new MyClass();” to:

    MyClass cl2 = (MyClass)cl1.clone();
    :-)

    Brendan Bates
    Posted December 2, 2008 at 6:44 am | Permalink

  43. 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);
    }

    Patrick
    Posted December 2, 2008 at 6:57 am | Permalink
  44. MyClass cl2 = (MyClass) cl1.clone();

    Yuri Schimke
    Posted December 2, 2008 at 8:11 am | Permalink
  45. 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();
    }

    }

    Andreas Groll, Michael Fürstenberg
    Posted December 2, 2008 at 10:20 am | Permalink
  46. private static final Object ocl = new Object();

    not that hard compared to some SCJP questions :)

    shamaz
    Posted December 2, 2008 at 10:32 am | Permalink
  47. 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();
    }
    }

    Christoph
    Posted December 2, 2008 at 10:35 am | Permalink
  48. shamaz,

    “private static final Object ocl = new Object();”

    is not correct because you are allowed to change ONLY main method.

    Posted December 2, 2008 at 11:24 am | Permalink
  49. 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.

    Posted December 2, 2008 at 2:44 pm | Permalink

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Anti-Spam Protection by WP-SpamFree