• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

help on another strange question.

 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why can program 1 work, but program 2 cannot?
Program1:
-------------------------
public class OuterTest{
public static void main(String args[]){
OuterTest test = new OuterTest();
test.processBoolean(false);
}
void processBoolean(boolean flag)
{
final int x;
if (flag){
x =9;
System.out.println(x);
}
else{
x=7;
System.out.println(x);
}
}
}
program 2:
_________________________
public class OuterTest{
public static void main(String args[]){
OuterTest test = new OuterTest();
test.processBoolean(false);
}
void processBoolean(boolean flag)
{
final int x;
if (flag){
x =9;
System.out.println(x);
}


if (!flag){
x = 27;
System.out.println(x);
}
}
}
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think the compiler expects an 'else' statement
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, for final variables, you should always initialise it before using it. If the declaration and initialisation are not in the same statement, the compiler will make sure that you initialise it properly, otherwise it complains.
The point is that the kind CHECK that the compiler gave is simple one, like <code>if(){} else{}</code>, it will not check relatively complex logic, like case 2.
Correct me if i am wrong, hope it helps.
Guoqiao
 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler error goes like this :
Can't assign a second value to a blank final variable: x
x=7;
My understanding of the subject is :
- if...else is used when u want either this or that (so you have only one option).
- two ifs can be used for two different cases.
- the modifier used for 'x' is final. If a final variable (in other terms 'constant') may not be modified once it has been assigned a value. In this case, final variable is already assigned a value of 9. A second value of 7 will not be acceptable to 'x'. Hence the error.
- Either use if....else in the program or remove the final modifier and the output will be 7.
Am I right?
- Paddy
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
As it is said that final variables should be declared and initialized in one line OR final varible should be initialized before using it and ONE MORE THING which is important here for this question that is the value of the final variable can not be changed once assigned. We are violating this rule here in this program by assigning the value to final varible in both if statements (CASE 2) because both the if statements are independant and compiler does not know at compile time that wether one or both will execute that's why it objects that the final variable is being initialzed twice. While in CASE 1, compiler knows by the combination of if, else that only one block will be executed that's why it does not give error in that case.
Hope this explanation will clear all doubts.
Asif Masood
 
I am mighty! And this is a mighty small ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic