About Question enthuware.ocajp.i.v8.2.1397 :
Moderator: admin
-
- Posts: 2
- Joined: Fri Sep 11, 2015 4:37 pm
- Contact:
About Question enthuware.ocajp.i.v8.2.1397 :
int dx, dy are local variables. attempting to use them in the try block results in a compile error, therefor the correct answer is "It will not compile".
-
- Site Admin
- Posts: 9791
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1397 :
Did you try compiling and running the code?
If you like our products and services, please help us by posting your review here.
-
- Posts: 14
- Joined: Sat Dec 17, 2016 10:17 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1397 :
Is my understanding correct: A local variable can be initialized in a try catch block (But only if it's initialized in both the try block and the catch block. If it's only initialized in the try block but not the catch block, the file wouldn't compile.)
-
- Site Admin
- Posts: 9791
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1397 :
Yes, that is correct. But you should really write small test programs to test your understanding.
HTH,
Paul.
Code: Select all
public class TestClass{
public static void main(String args[]){
int i;
try{
i = 10;
}catch(Exception e){
i = 20; //Comment it out and see what happens.
}
System.out.println(i);
}
}
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 6
- Joined: Sat Feb 04, 2017 6:18 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1397 :
But why with finally it will work?admin wrote:Yes, that is correct. But you should really write small test programs to test your understanding.

Code: Select all
public class TestClass{
public static void main(String args[]){
int i;
try{
i = 10;
}finally{
}
System.out.println(i);
}
}
-
- Site Admin
- Posts: 9791
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1397 :
The rule is that a variable must definitely be initialized before it can be accessed.
If the try block has no catch block:
the compiler knows that if the code in try block throws an exception, even println statement will not be executed.So even if i is not initialized, it is ok because i will not be accessed.
If you change the code to :
This will fail to compile because if the code in try block throws an exception, i =10 will not execute and the exception will be caught by the catch block. The call to println will be executed. But at this point i is uninitialized due to the exception.
HTH,
Paul.
If the try block has no catch block:
Code: Select all
try{
i =10;
}
finally{
}
System.out.println(i);
If you change the code to :
Code: Select all
try{
i =10;
}
catch(Exception e){
}
System.out.println(i);
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 6
- Joined: Sat Feb 04, 2017 6:18 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1397 :
I get it now, thanks!
-
- Posts: 202
- Joined: Mon Apr 02, 2018 8:40 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1397 :
Code: Select all
public class Temp{
public static void main(String args[]){
int i;
try{
i = 10;
}catch(Exception e){
//i = 20; //Comment it out and see what happens.
}
System.out.println(i);
}
}
-
- Site Admin
- Posts: 9791
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1397 :
You need to use the command prompt to compile and run it. Very difficult to identify the issue when you are using an IDE.
If you like our products and services, please help us by posting your review here.
-
- Posts: 202
- Joined: Mon Apr 02, 2018 8:40 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1397 :
Code: Select all
Seba@DESKTOP-SFBROA1 MINGW64 ~/Desktop
$ javac -version
javac 1.8.0_161
Seba@DESKTOP-SFBROA1 MINGW64 ~/Desktop
$ javac Temp.java
Temp.java:10: error: variable i might not have been initialized
System.out.println(i);
^
1 error
Seba@DESKTOP-SFBROA1 MINGW64 ~/Desktop
$ java Temp
10
BTW. I am not using an IDE but notepad++ (it can compile and run java code).
-
- Site Admin
- Posts: 9791
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1397 :
Yes, the code must compile first so that a class file is produced, which can then be run. So, the only inference that can be drawn here is that you have an old class file lurking around in that directory. Delete it. In fact, just delete all .class files using del *.class
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 2 guests