Local variables
"Greetings, my beloved student! It's time to get a little more serious about variables. But this time we won't discuss their internal structure. Instead, we'll focus on how variables interact with the code where they are located.
"All variables that are declared inside methods are called local variables. A local variable exists only in the block of code in which it is declared. Or, to be more precise, it exists from the moment it is declared until the end of the block of code in which it is declared."
"Clearly, I need an example."
"No problem. Here you go:
Code | Variable availability |
---|---|
|
|
"Let's go over the rules for accessing local variables one more time. Here is a block of code. What marks its beginning and end?"
"Curly braces?"
"Correct. This could be a method body, the body of a loop, or just a block of code for a conditional statement. A variable declared in a block of code exists until the end of that block of code. Tell me, where will a variable exist if it is declared in the body of a loop?"
"It will exist only in the body of the loop."
"That's right. What's more, it will be created and destroyed at each iteration of the loop."
"That means that you cannot declare two local variables with the same name in one method — the program will not compile. But you can do this if the blocks of code where the variables are declared do not overlap."
"There's a reason why you're my favorite, Amigo. Take one more example to solidify this in your brain.
Code | Variable visibility |
---|---|
|
|
"We were able to declare a second local variable named b
only because the first b
variable is not visible in the code block where the second b
variable is declared.
Parameters
"As we said before, each method can have variables that we call parameters. What about their visibility and lifetime?"
"Hmm... I'm stumped..."
"Everything is quite simple here. Parameters are created when execution steps into the method (i.e. when the code of the method starts executing). They are eliminated when the method ends."
"In other words, they are visible in the entire body of the method and only there?"
"Yes. Example:
Code | Variable visibility |
---|---|
|
|
"As we said earlier, args
is just a variable whose type is an array of strings. And like all parameters, it is available everywhere within the body of the method. That said, we usually ignore it in our our examples.
Variables in a class
"Remember the lessons in Level 1, where we said that a class can have methods and variables. Methods are sometimes called instance methods, and variables — instance variables or fields.
"What are the variables (or fields) of a class?
They are variables that are declared not in a method, but in a class."
"And what are they there for?"
"For starters, they can be accessed from any (non-static) method of a class. Roughly speaking, instance variables are variables that are shared by all the methods of a class.
Example:
Code | Variable visibility |
---|---|
|
|
"In this example, we have two methods — add()
and remove()
. The add()
method increments the sum
and count
instance variables, and the remove()
method decreases the sum
and count
variables. Both methods work on shared instance variables."
"I understand all of it! Local variables exist while a method is executing. The instance variables of a class exist within an object of a class as long as that object exists."
"Well done, Amigo. We've laid some groundwork, and you'll learn details about objects of a class in the next level.
Static variables
"Like methods, the variables in a class can be static or non-static. Static methods can only access static variables.
"I don't have a clear understanding of static variables yet."
"Oh, I know, but don't worry. For now, just get comfortable with them. Get familiar with them. In Level 11, we'll analyze the structure of static variables and methods and you'll understand the reasons for these restrictions.
"To make a static variable (class variable), you must write the static
keyword in its declaration.
"Static variables are not bound to an object or instance of the class in which they are declared. Instead, they belong to the class itself. That's why they exist even if not a single object of the class has been created. You can refer to them from other classes using a construct like:
ClassName.variableName
Example:
Code | Variable visibility |
---|---|
|
|
"In the above example, we created a separate Storage
class, moved the count
and sum
variables into it, and declared them static. Public static variables can be accessed from any method in a program (and not only from a method)."
"I don't fully understand, but this seems convenient to me."
"So it is. And sometimes it is necessary. Without static variables and methods we would be stuck."
"Hopefully, I'll slowly be able to figure it out."
"Yes, of course you will."
GO TO FULL VERSION