Showing posts with label Java Variables. Show all posts
Showing posts with label Java Variables. Show all posts

Monday, 21 September 2015

Java Tutorial : Java Variable Basics

Master Core Java Concepts!

Subscribe to Ram N Java for clear explanations, practical code examples, and essential Java tutorials!

Subscribe Now on YouTube

What Is a Variable in Java?

In Java, a variable is simply the name of a reserved area allotted in memory (RAM). When you declare and initialize a variable, the Java Virtual Machine allocates a memory slot to hold your value and assigns your chosen variable name to that memory location.

How Variables Work in Memory

Consider the following simple variable declarations:

int age = 30;
long salary = 50000;

Here is what happens behind the scenes during execution:

  • int age = 30; — Once executed, a reserved area is allocated in Random Access Memory (RAM). The value 30 is stored inside, and the reserved memory location is named age.
  • long salary = 50000; — A separate reserved area in RAM is created to store the value 50000, and the name assigned to that memory area is salary.

Key Takeaways

✔️ Definition: A variable is a named memory location used to store data temporarily during program execution.

✔️ Data Types Determine Allocation: The data type (like int or long) dictates how much memory is reserved.

✔️ Direct Reference: You refer to the stored data in your code using the variable name rather than raw memory addresses.


Watch More Java Tutorials:

Tuesday, 15 September 2015

Java Tutorial : Java Variables

Master Core Java Concepts!

Subscribe to Ram N Java for clear explanations, practical code examples, and essential Java tutorials!

Subscribe Now on YouTube

Types of Variables in Java

A variable is a container that holds data during the execution of a Java program. In Java, variables are categorized into three main types based on where and how they are defined:

  1. Instance Variables: Declared inside a class but outside of any method, constructor, or block, without the static keyword. Each created object gets its own copy.
  2. Static Variables: Declared inside a class with the static keyword. Only one copy exists for the entire class, shared across all instances.
  3. Local Variables: Declared inside a method, constructor, or block. They are accessible only within that specific scope.

Code Example: Defining Java Variables

class Student {
  // Instance variable
  String name;

  // Static variable
  static String schoolCode = "SCH101";

  public void eat() {
    // Local variable
    int numberOfBiscuits = 10;
    System.out.println("Number of biscuits eaten: " + numberOfBiscuits);
  }
}

Accessing Different Variables in Java

public class StudentDemo {
  public static void main(String[] args) {
    // 1. Accessing instance variable via object reference
    Student student = new Student();
    student.name = "John";
    System.out.println("Name: " + student.name);

    // 2. Accessing static variable directly via class name
    System.out.println("School Code: " + Student.schoolCode);

    // 3. Calling method containing local variable
    student.eat();
  }
}

Key Takeaways

✔️ Instance Variables: Tied to class objects; created when an object is instantiated.

✔️ Static Variables: Loaded when the class is loaded; best accessed using ClassName.variableName.

✔️ Local Variables: Temporary variables destroyed as soon as method execution finishes.


Watch More Java Tutorials:

Tutorials