0

I have method printB():

private static final void printB () {
    System.out.println(Boolean.TRUE); 
    System.out.println(Boolean.FALSE);     
}

and I have class

public class ChangeBooleanValue {
    public static void main(String[] args) {
        // Add code
        printB();      
    }


    private static final void printB () {
        System.out.println(Boolean.TRUE);     
        System.out.println(Boolean.FALSE); 
    } 
}

I want to write code instread of // Add Code so that the output will be:

true 
true

or

false
false

I know that it is possible using by some Java tricks.

arshajii
  • 127,459
  • 24
  • 238
  • 287
G.Ch.
  • 35
  • 6

4 Answers4

2

You can do something like:

System.out.println(true);
System.out.println(true);
System.exit(0);

You can do the same thing with false as well.


The full code would look like this:

public class ChangeBooleanValue {
    public static void main(String[] args) {
        System.out.println(true);  // <--
        System.out.println(true);  // <--
        System.exit(0);            // <--
        printB();      
    }


    private static final void printB() {
        System.out.println(Boolean.TRUE);     
        System.out.println(Boolean.FALSE); 
    } 
}
arshajii
  • 127,459
  • 24
  • 238
  • 287
  • 1
    or `System.out.println(!true);` ;) – Thomas Sep 02 '13 at 13:05
  • Task is: "don't change code which is written. add but not change code which is written" – G.Ch. Sep 02 '13 at 13:25
  • @G.Ch. This *doesn't* change anything which is already written, it is simply a substitute for the `Add code` comment (as you asked for). – arshajii Sep 02 '13 at 13:26
  • this code is generating output like this : true false I want to add code which will generate output where will be written two same values. not three , not one. – G.Ch. Sep 02 '13 at 13:32
  • @G.Ch. Copy and paste this where you have written `Add code`; the output will be `true true` (on separate lines). – arshajii Sep 02 '13 at 13:37
  • and when I invoke method it also generate output with two values – G.Ch. Sep 02 '13 at 13:48
  • @G.Ch. I've added the full code, which satisfies the requirements you've detailed in your question. If there is something else that you have not mentioned, please add it to your question. – arshajii Sep 02 '13 at 15:02
1

You can use reflections to change Boolean.TRUE = Boolean.FALSE or visa-versa. Not that there is a sane reason to do so.

Another way to do this is to compile your own version of Boolean class and use that to do whatever you want, again not at all sane.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
1

You use reflection to remove the final modifier and replace Boolean.FALSE with true.

Described in this answer: https://stackoverflow.com/a/3301720/131773

import java.lang.reflect.*;

public class EverythingIsTrue {
   static void setFinalStatic(Field field, Object newValue) throws Exception {
      field.setAccessible(true);

      Field modifiersField = Field.class.getDeclaredField("modifiers");
      modifiersField.setAccessible(true);
      modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);

      field.set(null, newValue);
   }
   public static void main(String args[]) throws Exception {      
      setFinalStatic(Boolean.class.getField("FALSE"), true);

      System.out.format("Everything is %s", false); // "Everything is true"
   }
}
Community
  • 1
  • 1
OliverS
  • 1,251
  • 12
  • 17
1

The code from OliverS:

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

public class ChangeBooleanValue {
    public static void main(String[] args) {
        // Add code
        try {
            Field f = Boolean.class.getField("FALSE");
            f.setAccessible(true);

            Field modifiersField = Field.class.getDeclaredField("modifiers");
            modifiersField.setAccessible(true);
            modifiersField.setInt(f, (f.getModifiers() & ~Modifier.FINAL));
            f.set(null, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        printB();
    }

    private static final void printB() {
        System.out.println(Boolean.TRUE);
        System.out.println(Boolean.FALSE);
    }
}

Output:

true
true
Community
  • 1
  • 1
Khinsu
  • 1,487
  • 11
  • 27