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
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Why is this thread closed?

 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Please consider the follwing code

code:

public class Fiz
{
int x = 5;

public static void main(String[] args)
{int x;
final Fiz f1 = new Fiz();
Fiz f2 = new Fiz();

Fiz f3 = Fizswitch(f1,f2);

if(false){ //line 1
f1=f2;//line 2
System.out.print("sdfdsf");
}

System.out.println(f1.x = f3.x);
}

static Fiz Fizswitch(Fiz x, Fiz y){
final Fiz z =x;
z.x =6;
return z;
}

}



at line 2 i get a compiler error: cannot assign a value to final variable f1
f1=f2;
^
1 error

why cant the compiler tell that the if block will not run even with a literal value of false in the if test condition? where as in the below code it can

code:

public class TestLocal {
public static void main(String [] args) {
int x;
boolean b = true;
boolean c = b;
if (false) { // always false
x = 7; //
}
int y = x;//line 1

System.out.print(x);
}
}



at line1 i get compiler error : variable x might not have been initialized
int y = x;
^
1 error

In the first case compiler still able to get inside the if(false) condition but in the second case compiler not able to get inside the if(fasle) condition...can somebody explain please? thanks
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Read the top of that closed version. it quite clearly says

This topic has been MOVED to another forum: Java in General (intermediate)
Please CLICK HERE to go to the new location.


I imagine this thread will also be closed very soon...
 
sridhar row
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
thanks fred. i guess its time for me to get a new pair of glasses
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Please continue over there.
 
The overall mission is to change the world. When you've done that, then you can read this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
    Bookmark Topic Watch Topic
  • New Topic