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

  • Roland Tepp

    The most obvious answer would be:

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

  • dominikg

    My 0.02 are on “cl2=(MyClass)cl1.clone();” right before the println.

    dominikg

  • Tristan

    is it the good answer ?
    MyClass cl2 = (MyClass) cl1.clone();

  • http://pct.sourceforge.net Gilles QUERRET

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

    Gilles QUERRET

  • fillg1

    change line 2 in main to

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

  • Sergiy

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

  • http://dow.ngra.de Toomas Römer

    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);
    }
    ——————————————

  • http://datenreisender.de/ marko schulz

    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.

  • http://www.ed-merks.blogspot.com/ Ed Merks

    Add this line as the first line of main.

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

  • http://manuelselva.wordpress.com/ Manuel Selva

    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

  • Ancuta

    Make cl2 a clone of cl1:

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

  • http://www.tutorials.de Thomas Darimont

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

    }

  • Robert Munteanu

    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

  • DrMastaP

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

  • koala


    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…

  • Daniel

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

    }

  • Johan

    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

  • kmast

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

  • Yuriy

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

    }

  • http://borisoneclipse.blogspot.com Boris Bokowski

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

  • http://www.gmo-web.info Guillaume Mouron

    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();

  • http://cubussapiens.hu Balázs Grill


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

  • Joakim E

    // In diff format.
    - MyClass cl2 = new MyClass();
    + MyClass cl2 = (MyClass) cl1.clone();

  • Cole

    One more reason why I try to avoid the clone method.

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

  • Jerome Lanneluc


    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

    Oops, I changed the sysout line. So much to play the smart guy :-)

  • Jean Palies

    Something like MyClass cl2 = cl1.clone();

  • Holger

    Change line 4 to:
    MyClass cl2 = (MyClass)cl1.clone();
    Is there a smaller change? I can see no other way without editing MyClass

  • Daniel

    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

  • http://www.puzzle.ch André Dietisheim


    private final Object ocl = “aString”;

  • Phillipus

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

  • http://andymaleh.blogspot.com/ Andy Maleh


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

    }

  • Chris

    I would change the line that reads, “MyClass cl2 = new MyClass();” to be MyClass cl2 = (MyClass) cl1.clone();”

  • Kiril Mitov

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

    }

  • Michael

    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…

  • H

    Change:
    MyClass cl2 = new MyClass();
    to:
    MyClass cl2 = (MyClass)cl1.clone();

  • http://www.mhaller.de/ mhaller

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

  • Radoslaw Jozwik

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

  • Dave Weatherford

    Replace the 4th non-blank line with this:

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

    Do I win???

  • http://www.cse.csiro.au Stephen Egan

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

    }

  • Alex Smirnoff

    Changing to

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

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

    Thanks,
    Alex.

  • Brendan Bates

    Change “MyClass cl2 = new MyClass();” to:

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

  • Patrick


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

  • Yuri Schimke

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

  • Andreas Groll, Michael Fürstenberg

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

    }

  • shamaz

    private static final Object ocl = new Object();

    not that hard compared to some SCJP questions :)

  • Christoph

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

  • http://ahtik.com Ahti

    shamaz,

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

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

  • http://datenreisender.de/ marko schulz

    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.