Skip to content

Scope and Constants

Constants

In Java, constants are special variables whose values cannot be changed once they are initialized. This is useful when you have a value that you know will never change, such as the value of pi or the maximum speed of your robot. Constants are typically declared using the final keyword followed by the data type, name, and initial value. For example, you could declare a constant for the maximum speed of your robot like this:

final double MAX_SPEED = 5.0;

Once you have declared this constant, you cannot change its value. If you try to do so, the Java compiler will give you an error. This helps to prevent bugs where you accidentally change a value that should remain constant.

Scope of Variables

The scope of a variable in Java refers to the region of code where the variable can be accessed. The scope is determined by where a variable is declared. For example, if a variable is declared inside a method, it is a local variable and can only be used within that method:

public class Main {
    public static void main(String[] args) {

        // Code here CANNOT use x

        int x = 100;

        // Code here can use x
        System.out.println(x);
    }
}

On the other hand, if a variable is declared as a class attribute, it is a class (or instance) variable and can be used by all methods in the class:

public class Main {
    public static void main(String[] args) {

        // Code here CANNOT use x

        { // This is a block

        // Code here CANNOT use x

        int x = 100;

        // Code here CAN use x
        System.out.println(x);

        } // The block ends here

    // Code here CANNOT use x

    }
}

Tasks

For this week's tasks, we'll be practicing working with variables and data types.

  1. At the top of your program, declare a constant named PI that has a double type and assign it the value of 3.14.

  2. Declare a variable of type double named radius and assign it a value of 5.0.

  3. Calculate the area of a circle using the formula PI * radius * radius and assign the result to a double variable named area. Print the value of area.

  4. Now, change the value of radius to 10.0 and calculate the area again, storing the result in the area variable. Print the value of area again. This should demonstrate how variables can be reassigned.

  5. Declare two String variables, teamMemberName and teamName. Assign any names you like to these variables.

  6. Declare a String variable greeting and assign a personalized greeting message that includes both teamMemberName and teamName using String concatenation. For example, the message could be "Hello, " + teamMemberName + "! Welcome to team " + teamName + "." Print the greeting.

  7. Reassign different values to teamMemberName and teamName, then create a new greeting message by again assigning to the greeting variable. Print the new greeting. This will show that you can update the contents of a string variable and print out new information.

Additional Resources

W3 Schools - Java Scope

W3 Schools - Variables