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

Moderator: admin

Post Reply
UmairAhmed
Posts: 9
Joined: Mon Apr 28, 2014 9:16 am
Contact:

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

Post by UmairAhmed » Thu May 22, 2014 12:50 pm

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

admin
Site Admin
Posts: 7556
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Thu May 22, 2014 7:54 pm

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.
If you like our products and services, please help us by posting your review here.

UmairAhmed
Posts: 9
Joined: Mon Apr 28, 2014 9:16 am
Contact:

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

Post by UmairAhmed » Fri May 23, 2014 5:35 pm

Thanks Paul

dmcinnis1
Posts: 16
Joined: Wed Feb 25, 2015 8:52 pm
Contact:

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

Post by dmcinnis1 » Fri Feb 27, 2015 12:57 pm

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?

admin
Site Admin
Posts: 7556
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Fri Feb 27, 2015 8:37 pm

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.
If you like our products and services, please help us by posting your review here.

JaredTse
Posts: 20
Joined: Sat Apr 23, 2016 2:52 pm
Contact:

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

Post by JaredTse » Fri May 27, 2016 10:03 am

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";

admin
Site Admin
Posts: 7556
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Fri May 27, 2016 12:38 pm

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.
If you like our products and services, please help us by posting your review here.

flex567
Posts: 113
Joined: Mon Apr 02, 2018 8:40 am
Contact:

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

Post by flex567 » Thu Sep 13, 2018 3:10 pm

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.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 9 guests