Java Revision Question

pshortland
pshortland used Ask the Experts™
on
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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
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: http://www.cs.umd.edu/~clin/MoreJava/Intro/java-prog2.html

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