Page 1 of 1

About Question enthuware.ocajp.i.v7.2.1118 :

Posted: Thu May 22, 2014 12:50 pm
by UmairAhmed
Hi,
I need some explanation about this question,
class SomeClass{
   String s1 = "green mile";   // 0
   public void generateReport( int n ){
      String local;   // 1
      if( n > 0 ) local = "good";   //2
      System.out.println( s1+" = " + local );   //3
   } }
options in answer say :
1). Insert else "bad" (Which is fine as my understanding)
2). Move line 1 and place it after 0, as it local variable inside the method. (???)

If i compile this code, by adding the main method in the above code like this
class SomeClass{
   String s1 = "green mile";   // 0
   public void generateReport( int n ){
      String local;   // 1
      if( n > 0 ) local = "good";   //2
else local = "bad";
      System.out.println( s1+" = " + local );   //3
   }
public static void main(String args[]) {
SomeClass aa = new SomeClass(); // for using the generateReport() method
aa.generateReport(5);
}}
This code works fine even we are not making automatic variable "local" as instance variable.
Isn't the second option redundant ... as this question can be answered completely with 1st option only.

Please explain if I am missing something with respect to this question.
Regards
/Umair

Re: About Question enthuware.ocajp.i.v7.2.1118 :

Posted: Thu May 22, 2014 7:54 pm
by admin
The problem statement says, "Which of the changes given in options can be done (independent of each other)...". So both the changes are not to be done together. Either of the changes will make it work.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1118 :

Posted: Fri May 23, 2014 5:35 pm
by UmairAhmed
Thanks Paul

Re: About Question enthuware.ocajp.i.v7.2.1118 :

Posted: Fri Feb 27, 2015 12:57 pm
by dmcinnis1
I still do not understand why the second option doesn't work. I don't understand why the compiler has a problem with it. Wouldn't the compiler look at both statements independently of each other?

Re: About Question enthuware.ocajp.i.v7.2.1118 :

Posted: Fri Feb 27, 2015 8:37 pm
by admin
That is how the Java compiler is designed. It cannot execute the code it can only make inferences by looking at information available at compile time. In the second option, the two ifs are independent and therefore, the compiler cannot assume that i will be initialized after the second if.

Re: About Question enthuware.ocajp.i.v7.2.1118 :

Posted: Fri May 27, 2016 10:03 am
by JaredTse
Can someone please elaborate on this, as to why is wrong. I can't seem to wrap my head around it.

Insert after line 2 : if(n <= 0) local = "bad";

Re: About Question enthuware.ocajp.i.v7.2.1118 :

Posted: Fri May 27, 2016 12:38 pm
by admin
Can you elaborate what exactly is causing the confusion to you so that we can help?
Also, did you try compiling this option? What error message do you get?
-Paul.

Re: About Question enthuware.ocajp.i.v7.2.1118 :

Posted: Thu Sep 13, 2018 3:10 pm
by flex567
So the compiler works like this:
If there is at most 1 statement that would initilize the variable, then compiler allows the uninutilize variable otherwise it doesn't.

Re: About Question enthuware.ocajp.i.v7.2.1118 :

Posted: Fri Jul 16, 2021 6:22 am
by enthunoob
I find it hard to understand how far the compiler can go to check if a local variable will be initialized. I understand it can go to into max 1 if-else function, but are there other functions it can look into? For example a for loop function? Or in a print function?
For example:
public static void localMethod() {
String A;
String B = "b";
System.out.println(A = "x");
}
I tried it out and the compiler does not complain. It is able to see that the local variable A is initialized within the print call. This also made me wonder if replacing "x" with B will matter, as A is then assigned to a variable instead of a compile time constant (and it doesn't matter).

I guess what I mean to ask: what is the exact 'depth' a compiler can check?

Re: About Question enthuware.ocajp.i.v7.2.1118 :

Posted: Fri Jul 16, 2021 7:07 am
by enthunoob
I tested into it a bit more and a for-loop never works. But surprisingly a switch statement does (as long as a default case is given)..

public static void localMethod() {
boolean bool = true;
String A;
// if (bool) A = "abc"; //compile error
// if (bool) A = "abc"; else A = "abc"; //OK
// for(int i = 0; i<2;i++){A = "abc";}; //compile error
// for(int i = 1; i==1;i=1){A = "abc";};//compile error
// A = bool ? "a" :"b"; //OK
// A = bool ? !bool ? "a" : "b" : "c"; //OK
// A = bool ? !bool ? "a" : bool ? "a" : "b" : "c"; //OK, same goes for nested if

final int i = 1;
switch (i){
case 0: A = "0"; break;
case 1: A = "1"; break; //even though i is final and the case 1 is here, it doesn't work if there is no default case.
default: A = "def"; //Only works when switch has default!
}

System.out.println(A);
}

Re: About Question enthuware.ocajp.i.v7.2.1118 :

Posted: Fri Jul 16, 2021 7:44 am
by admin
For the kind of precision that you are looking for, please go through Chapter 16 (Definte Assignment) of JLS carefully: https://docs.oracle.com/javase/specs/jl ... ls-16.html

Re: About Question enthuware.ocajp.i.v7.2.1118 :

Posted: Sat Oct 09, 2021 3:35 am
by Syams123
Hi,

What about n variable which is not explicitly initialized so how we are going to evaluate if statement without declaring n value? And even though string local value is not initialized at first its assigned value through if/else statement right?

Can please someone let me know.

Thanks

Re: About Question enthuware.ocajp.i.v7.2.1118 :

Posted: Sat Oct 09, 2021 4:37 am
by admin
n is a method parameter. It is always initialized by the value sent by the caller of this method. The caller has to send a value when invoking this method otherwise the code will not compile.