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
public static void main(String[] args)
{
   int a = 5;
   if (a < 10)
   {
     int b = 10;
     while (true)
     {
       int x = a + b;
       System.out.println(x);
     }
     System.out.println(b);
   }

}


a
a
a
a, b
a, b
a, b
a, b, x
a, b, x
a, b
a, b
a
a

"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
public static void main(String[] args)
{
   int a = 5;
   if (a < 10)
   {
     int b = 10;
     System.out.println(b);
   }

   if (a < 20)
   {
     int b = 20;
     System.out.println(b);
   }
}


a
a
a
a, b
a, b
a
a
a
a
a, b
a, b
a

"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
public static void main(String[] args)
{
   int a = 5;
   if (a < 10)
   {
     int b = 10;
     while (true)
     {
       int x = a + b;
       System.out.println(x);
     }
     System.out.println(b);
   }

}

args
args, a
args, a
args, a
args, a, b
args, a, b
args, a, b
args, a, b, x
args, a, b, x
args, a, b
args, a, b
args, a
args, a

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

undefined
7
Task
New Java Syntax, level 7, lesson 6
Locked
Everything has a root
The printSqrt(int[] array) method should print the square root of each element in the passed array. But this isn't happening due to conflicting variable names. Correct the variable names so that the code compiles. The program should display a suitable line on the console for each element of the arra

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
public class Solution
{
   public int count = 0;
   public int sum = 0;

   public void add(int data)
   {
     sum = sum + data;
     count++;
   }

   public void remove(int data)
   {
     sum = sum - data;
     count--;
   }
}


count
count, sum
count, sum
count, sum
count, sum, data
count, sum, data
count, sum, data
count, sum
count, sum
count, sum
count, sum, data
count, sum, data
count, sum, data
count, sum
count, sum

"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
public class Solution
{
   public void add(int data)
   {
     Storage.sum = Storage.sum + data;
     Storage.count++;
   }

   public void remove(int data)
   {
     Storage.sum = Storage.sum - data;
     Storage.count--;
   }
}

public class Storage
{
   public static int count = 0;
   public static int sum = 0;
}

Storage.count, Storage.sum
Storage.count, Storage.sum
Storage.count, Storage.sum, data
Storage.count, Storage.sum, data
Storage.count, Storage.sum, data
Storage.count, Storage.sum
Storage.count, Storage.sum
Storage.count, Storage.sum
Storage.count, Storage.sum, data
Storage.count, Storage.sum, data
Storage.count, Storage.sum, data
Storage.count, Storage.sum



Storage.count, Storage.sum
Storage.count, Storage.sum
Storage.count, Storage.sum

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

undefined
7
Task
New Java Syntax, level 7, lesson 6
Locked
Giant cities
The program should display the population of the largest cities in the world and compare them with Tokyo, the most populous city of all. But the program's algorithm is slightly broken. To correct the error, you need to make it so the line that displays information about the world's largest city uses