Java Revision Question

pshortland used Ask the Experts™
I am doing some Java revision and a past exam paper poses the following question, which I can not answer !

Q What is the result of declaring an instance variable and a local variable with the same name ?

I am thinking the two will cause the code to fail to compile but am not 100% convinced.
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Had you tried to run a program to check it? :) This is the easiest way to learn such things.

The code will compile - there is no reason not to as there is a clear rule on what will be used in which case.

This is a not so bad paper on the declaring of variables in Java:

Towards the end is also your answer - the local one will be used if there is a local and instance ones with the same name at the same part of the code.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial