About Question enthuware.ocajp.i.v8.2.1397 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
joestewart
Posts: 2
Joined: Fri Sep 11, 2015 4:37 pm
Contact:

About Question enthuware.ocajp.i.v8.2.1397 :

Post by joestewart »

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

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

Re: About Question enthuware.ocajp.i.v8.2.1397 :

Post by admin »

Did you try compiling and running the code?
If you like our products and services, please help us by posting your review here.

eddie3
Posts: 14
Joined: Sat Dec 17, 2016 10:17 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1397 :

Post by eddie3 »

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.)

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

Re: About Question enthuware.ocajp.i.v8.2.1397 :

Post by admin »

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;
        }catch(Exception e){
          i = 20; //Comment it out and see what happens.
        }
        System.out.println(i);
   }
}
HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Maor.S
Posts: 6
Joined: Sat Feb 04, 2017 6:18 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1397 :

Post by Maor.S »

admin wrote:Yes, that is correct. But you should really write small test programs to test your understanding.
But why with finally it will work? :shock:

Code: Select all

public class TestClass{
   public static void main(String args[]){
        int i;
        try{
          i = 10;
        }finally{
        
        }
        System.out.println(i);
   }
}

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

Re: About Question enthuware.ocajp.i.v8.2.1397 :

Post by admin »

The rule is that a variable must definitely be initialized before it can be accessed.
If the try block has no catch block:

Code: Select all

try{
  i =10;
}
finally{
}
System.out.println(i);
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 :

Code: Select all

try{
  i =10;
}
catch(Exception e){
}
System.out.println(i);
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 you like our products and services, please help us by posting your review here.

Maor.S
Posts: 6
Joined: Sat Feb 04, 2017 6:18 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1397 :

Post by Maor.S »

I get it now, thanks!

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

Re: About Question enthuware.ocajp.i.v8.2.1397 :

Post by flex567 »

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);
   }
}
When I run the program it prints out 10 , but when I click compile it fails to compile.

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

Re: About Question enthuware.ocajp.i.v8.2.1397 :

Post by admin »

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.

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

Re: About Question enthuware.ocajp.i.v8.2.1397 :

Post by flex567 »

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
I thought that programs need to first compile to be able to run.??

BTW. I am not using an IDE but notepad++ (it can compile and run java code).

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

Re: About Question enthuware.ocajp.i.v8.2.1397 :

Post by admin »

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.

Post Reply

Who is online

Users browsing this forum: No registered users and 30 guests