Stephan van Hulst wrote:If you have lots of these questions, it may be worth your time to learn how to navigate the JLS. Even though it's technical, it is well written and has many links between related paragraphs.
Stephan van Hulst wrote:https://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.4.7
If a method is declared to have a return type, then a compile-time error occurs if the body of the method can complete normally (§14.1).
shambhavi sham wrote:In addition , I have a doubt regarding the behaviour of the following code snippets too :
CODE A :
Here, since 'if' is always true and determined at compile time itself, we know for sure that c and d will be initialized before its use . so it compiles fine. but when if is false i.e. if(false) will give an error since c and d will be used without initialization .
CODE B :
Similarly, over here at compile time, we know that if is true and so surely the return statement will get executed,similar to the above example. but yet, it shows an error and expects a return statement outside the if block too.
why are both the codes contradicting ?