Learn Java With Me: Day 3

Shayla White
3 min readMay 24, 2021

Third day is the charm. In today’s session we will learn about variables, and scope. I wanted to cover conditionals on today but I believe they really deserve their very on day, they are just that spectacular. So lets dive right in.

Java Variables

Variables are containers for storing data values.

In Java, there are different types of variables, for example:

  • String - stores text, such as "Hello". String values are surrounded by double quotes
  • int - stores integers (whole numbers), without decimals, such as 123 or -123
  • float - stores floating point numbers, with decimals, such as 19.99 or -19.99
  • char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes
  • boolean - stores values with two states: true or false

Understanding Scope

Scope

When working with Java understanding scope is very important.
Understanding scope is important because it helps to understand when a variable will be accessible to a certain line of code and when it will be inaccessible, the smaller the scope the less accessible the variable is.
There are three main levels of scope, class level/instance scope, method/local scope, and loop scope.

Class Level/Instance Scope : Referenceable throughout the entire class, these variables are inside the class but outside of methods. Generally these variables will be defined at the top of the class.

public class HelloWorld {    public static String message = "Hello World!";    public static void method1() {
System.out.println(message);
}
public static void main(String[] args) {
method1();
}
}

In the above example, message is defined and initialised as a String variable within the class scope, but outside of any method scope.
Therefore the variable can be referenced and used throughout the class freely.

Method/Local Scope : Variables that are temporary and generally only used in the method that they are declared in.
As soon as the method ends all variables declared inside that method are no longer referenced and cannot be accessed anymore.

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);
}
}

Hello World Example:

public class HelloWorld {    public static void method1() {
String message = "Hello World!";
System.out.println(message);
}
public static void main(String[] args) {
method1();
}
}

In the above example message is defined and initialised as a String variable inside a method’s scope, in this case inside the scope of method1().
The message variable will no longer be accessible once method1() has finished executing all of the lines of code within its scope.

Loop Scope : Variables that are declared inside a loop declaration and are only accessible inside the loop and are lost once the loop has ended.

public class HelloWorld {    public static void method1() {
for(int i = 0; i < 10; i++) {
int number = 20;
number += i;
System.out.println(number);
}
}
public static void main(String[] args) {
method1();
}
}

In the above example, number is defined and initialised within the for loop's scope.
The number variable will be accessible whilst Java is executing the code within the loop, but once the loop has finished it will no longer be accessible.

Block Scope

A block of code refers to all of the code between curly braces {}. Variables declared inside blocks of code are only accessible by the code between the curly braces, which follows the line in which the variable was declared:

Example

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 }
}

--

--