From the course: Oracle Java Certification: 1. Data Types

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Reference scope

Reference scope - Java Tutorial

From the course: Oracle Java Certification: 1. Data Types

Start my 1-month free trial

Reference scope

- [Instructor] I've decided to show you this particular video in the IDE. Remember that all the links to the code snippets are available in the handout in the Exercise Files. Right now I'm in file 02_05.1. In Java, every data item goes through a lifecycle, a variable or name starts to exist when it is created. A variable scope defines where in the program the variable is available for reference. Here the definition of variable a exists in two different scopes. Once in the Main class as instance variable at line three. And the other time in the print1 method as an input parameter which is on line 15. In this program, we use the two print methods to print out variable a. What would this code output? Let me run it in the IDE and take a look. It looks like it compiles correctly. How did this happen? Let's trace this program. The execution starts with the Main method. We create an object of the Main class and call its print methods. We have two definitions of a in this class. The first…

Contents