-3

I came across this question in interview, that how to make a variable constant without final keyword in java

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • rather stupid interview question, not a real need for doing something like this in real life. Constants in Interfaces are very, very bad code smells. It is good thing that nobody is programming in this way, it is even better to don't know/remember this, to not be tempted :) – Krzysztof Cichocki Jun 05 '17 at 11:37
  • @MadaraUchiha I don't think this question is too broad - it's specific enough in a context of a job interview to provide a definitive answer. – Sergey Kalinichenko Jun 05 '17 at 11:52

2 Answers2

3

I suspect that they were looking for an answer "declare your variable inside an interface". According to Java language specifications, all variables declared in an interface are implicitly static and final:

interface Foo {
    int BAR = 100;
}
class Test {
    public static void main (String[] args) throws java.lang.Exception
    {
        System.out.println(Foo.BAR);
    }
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Great answer, but what is the purpose of such interview question? Check if you are good programmer? Good programmers do not create Constants in Interaces in the first place. – Krzysztof Cichocki Jun 05 '17 at 11:42
  • 1
    @KrzysztofCichocki I am not the one making this interview question, so I cannot tell you the purpose of the question. To me, this is a legitimate Java language puzzle. – Sergey Kalinichenko Jun 05 '17 at 11:45
  • Ok, a have another great puzzle in same style: is it possible in java to throw checked exception from a method that dosn't declare it? Yes. But If you do it you are terrible programmer, so what value it brings that someone know answer to such question, when there is a lot more neccessery and important things to know? – Krzysztof Cichocki Jun 05 '17 at 11:49
  • @KrzysztofCichocki There is a value to interviewers in knowing that the candidate knows answers to such puzzles, because it shows them a certain breadth of learning. In a way, this is similar to memorizing swear words in a foreign language that you are studying: it's certainly true that you never use them in a conversation, yet there is value in recognizing them when they are spoken around you. – Sergey Kalinichenko Jun 05 '17 at 12:18
  • I don't think that such questions have any value as an indicator of the breadth of learning. Remeber that, in Java there is reflection, type erasure and tools for runtime byte code generation/modfication. This makes this question impossible to answer. Using these tools you can break almost any law of the java language. This is the single answer to all questions of this type. In java everything is possible! It is even possible to swap the real value true/false of Boolean to opposite! The interview questions should be rather about good programming practices and not stupid things. – Krzysztof Cichocki Jun 05 '17 at 12:32
  • @KrzysztofCichocki It's all relative. If I were the interviewer asking this question, I'd accept an answer with interface variables, and move on to my next question. If a candidate gave me an answer that it is not possible because constants cannot hide from reflection, and wrote code for swapping `Boolean.TRUE` with `Boolean.FALSE`, I would stop the interview, and offered the candidate a job. – Sergey Kalinichenko Jun 05 '17 at 12:42
  • People are sensitive to stress, stress reduces mental capabilities - it is the way in which brain is working, by desing. Can you stop your cells from splitting using your will? No? so you can't stop your brain from producing neurotransmitters. Of course, you can MOVE your legs faster under high stress to escape the danger - it is the natural purpose of stress, but you can't THINK, be CREATIVE or PRODUCTIVE under high stress. Does programmer need to stand high stress? It is so stupid to write in a job offer for a programmer that one of the requirements is "high stress resistance" – Krzysztof Cichocki Jun 06 '17 at 07:38
0

You can force such a variable to be effectively final. For example, use it in a stream.

int a = 1;
Stream.of(1, 2, 3, 4, 5).map(e -> e + a).forEach(System.out::println);

a = 12; // this line leads to a compile error
Yuri H
  • 680
  • 5
  • 12