Constants

"Amigo, did you know that in Java not all variables can be changed?"

"How's that, Kim? Indeed, the very word 'variable' speaks of change."

"There is no doubt about that. Like many other programming languages, Java has constants, that is, variables whose values cannot be changed. And the very word 'constant' speaks of constancy."

"And what are they for?"

"Usually, they are used for some kind of fundamental thing such as the number Pi or the number of days in the months of the year. That said, in principle, a programmer can make any variable a constant, if he or she decides that doing so is necessary."

"You mean like a name, color of a car, or name of a day of the week?"

"You've got the right idea. Anything that shouldn't be changed."

"And what do these constants look like in Java?"

"Java has a special keyword for them: final. Creating an immutable variable looks the same as creating an ordinary one. The only difference is that before the variable's type you need to write the word final, like this:

final Type name = value;

"What happens if you create a constant and then try to assign a different value to it?"

"That is the right question to ask! And the right answer is this: if you try to assign a different value to a final variable, then your program simply won't compile."

"What if you declare a final variable but don't assign a value to it?"

"There's no point in doing this, so it's also not allowed in Java. A final variable must be initialized when it is declared, that is, you must assign a value to it. There is one exception to this rule: you can move initialization of a static class variable into a constructor. But you'll learn out about that later.

"That said, not everything that is final is a constant. To reduce the number of keywords, Java developers use the word final for more than just declaring constants. final can also apply to methods and even classes. Methods declared as final cannot be overridden, and a class declared as final cannot be inherited."

"Uh... Overridden? Inherited? What language are you speaking now?"

"The language of object-oriented programming. You will get to it very soon. Until then, just enjoy the beautiful terminology."

"Okay. So, final can be put before variables, classes, and methods, and this keyword makes them immutable in some sense?"

"Yes. Furthermore, the final modifier may be added before any variables: local variables, method parameters, class fields, and static class variables.

"Here's the important thing to remember: final before a variable name is just protection against any changes to that variable. If a variable stores a reference to an object, then the object can still be changed."

"I don't quite understand."

"You will understand really soon. Here's an example:

final int[] data = {1, 2, 3, 4, 5, 6};

data = {6, 7, 8, 9};

data[0] = 0;
data[1] = 0;
data[2] = 0;
We create an array.

This is not allowed: the data variable is declared as final.

But you can do this.
And also this.

"Got it. That's tricky."

Global constants

"What do you think global constants are?"

"I guess global constants are probably like global variables, only constants?"

"Exactly. If you need to declare global constants in your program, create static class variables, and make them public and final. There is a special style for the names of such variables: they are written in all capital letters, with an underscore character used to separate words.

Examples:

class Solution
{
   public static final String SOURCE_ROOT = "c:\\projects\\my\\";
   public static final int DISPLAY_WIDTH = 1024;
   public static final int DISPLAY_HEIGHT = 768;
}
undefined
7
Task
New Java Syntax, level 7, lesson 7
Locked
Remembering geometry
Take a look at what the program does. Add the final modifier to standard constants (variables with immutable values).
undefined
7
Task
New Java Syntax, level 7, lesson 7
Locked
Everything you need to know about Earth
Here is the Earth class — a collection of variables that describe certain characteristics of the planet Earth. Be sure that the variables of the Earth class are global constants. Don't forget to rename the variables so that they comply with code style conventions for global variables.

Variable shadowing

"As I said before, you cannot create multiple local variables with the same names in a single method. In different methods, you can."

"I know that!"

"But what you probably don't know is that variables in a class and local variables in a method may well have the same name.

Example:

Code Variable visibility
public class Solution
{
   public int count = 0;
   public int sum = 0;

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

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

"In the add method, we declared a local variable named sum. Until the end of the method, it shadows (or masks) the sum instance variable."

"Hmm... I would say in some sense this is the expected behavior."

"But that's not the end of the story. It turns out that if an instance variable is shadowed by a local variable, there is still a way to refer to the instance variable within the method. We do this by writing the this keyword before its name:

this.name

"Here's an example where the name conflict is successfully resolved:

Code Variable visibility
public class Solution
{
   public int count = 0;
   public int sum = 0;

   public void add(int data)
   {
     int sum = data * 2;
     this.sum = this.sum + data;
     count++;
   }
}

this.count, this.sum
this.count, this.sum
this.count, this.sum
this.count, this.sum
this.count, this.sum
this.count, this.sum, data
this.count, this.sum, data, sum
this.count, this.sum, data, sum
this.count, this.sum, data, sum
this.count, this.sum

The count and sum variables are available everywhere with or without the this keyword. On lines where the sum local variable shadows the sum instance variable, the sum instance variable can only be accessed using the this keyword.

"Obviously, I'll need to practice this."

"You'll manage."

"What if a static class variable is shadowed rather than just a (non-static) instance variable? You can't access it through this?"

"Right you are. The this keyword won't work. You need to refer to it through the class name:

ClassName.name

Example:

Code Variable visibility
public class Solution
{
   public static int count = 0;
   public static int sum = 0;

   public void add(int data)
   {
     int sum = data * 2;
     Solution.sum = Solution.sum + data;
     count++;
   }
}

Solution.count, Solution.sum
Solution.count, Solution.sum
Solution.count, Solution.sum
Solution.count, Solution.sum
Solution.count, Solution.sum
Solution.count, Solution.sum, data
Solution.count, Solution.sum, data, sum
Solution.count, Solution.sum, data, sum
Solution.count, Solution.sum, data, sum
Solution.count, Solution.sum

"Pay attention: you can access the count and sum static variables everywhere with or without using the class name Solution as a prefix. In those lines where the sum local variable shadows the sum instance variable, access to the sum class variable is possible only when using Solution as a prefix.

Variables inside a for loop

"And one more small but interesting fact. There's also a place where a variable is declared in a special way — I'm talking about inside a for loop." Typically, a for loop has a counter variable in parentheses. And what will be the visibility of this variable? After all, it's not in the body of the loop. Is it the whole method? Or not?"

"I've already heard something about this. As I understand it, a variable declared in the header of a for loop is visible only in the body of the loop and in the header of the for loop."

"Well done, Amigo. But still, have a look at an example to reinforce this material:

Code Variable visibility
public static void main(String[] args)
{
   int a = 0;

   for (int i = 0; i < 10; i++)
   {
     System.out.println(i);
   }

   System.out.println("end");
}


a
a
a, i
a, i
a, i
a
a
a

"So you're saying that in my code I could write several loops one after another with a counter variable with the same name, and there will be no problems?"

"There would be no problems. Here, look:

Code Variable visibility
public static void main(String[] args)
{
   int a = 0;

   for (int i = 0; i < 10; i++)
   {
     System.out.println(i);
   }

   for (int i = 0; i < 10; i--)
   {
     System.out.println(i);
   }

   System.out.println("end");
}


a
a
a, i
a, i
a, i
a
a
a, i
a, i
a, i
a
a
a

undefined
7
Task
New Java Syntax, level 7, lesson 7
Locked
Employee records
Here you have in front of you a digital employee record. It contains the employee's name, position and salary. The name of the employee is unlikely to change, but his or her position and salary might. These changes are made using the setPosition() and setSalary() methods. Of course, they are not wor