WikiGalaxy

Personalize

Java Variables

Introduction to Variables:

Variables in Java are used to store data that can be manipulated within a program. They have a type, name, and value, and are fundamental to programming in Java.


      int age = 25; // Variable declaration and initialization
      String name = "John"; // String variable
    

Primitive Data Types

Basic Types:

Java supports several primitive data types, including int, double, char, and boolean. Each type serves a specific purpose and has a fixed size.


      int number = 10;
      double price = 19.99;
      char grade = 'A';
      boolean isAvailable = true;
    

Variable Declaration and Initialization

Process:

Declaring a variable involves specifying its type and name. Initialization assigns an initial value to the variable. Both can be done simultaneously or separately.


      int score; // Declaration
      score = 100; // Initialization
      int level = 5; // Declaration and initialization
    

Local Variables

Scope:

Local variables are declared within a method or block and can only be accessed within that scope. They are not initialized by default and must be given a value before use.


      public void calculate() {
        int sum = 0; // Local variable
        sum = 5 + 3;
        System.out.println(sum);
      }
    

Instance Variables

Characteristics:

Instance variables are non-static fields defined within a class but outside any method. They are initialized when the class is instantiated and can be accessed by all methods within the class.


      public class Car {
        String model; // Instance variable
        int year; // Instance variable

        public Car(String model, int year) {
          this.model = model;
          this.year = year;
        }
      }
    

Static Variables

Shared Data:

Static variables, also known as class variables, are shared among all instances of a class. They are declared with the static keyword and exist independently of class instances.


      public class Counter {
        static int count = 0; // Static variable

        public Counter() {
          count++;
        }
      }
    

Final Variables

Constant Values:

Final variables are constants that cannot be changed once initialized. They are declared using the final keyword and must be initialized at the time of declaration or within a constructor.


      public class Circle {
        final double PI = 3.14159; // Final variable

        public double calculateCircumference(double radius) {
          return 2 * PI * radius;
        }
      }
    

Type Inference with var

Simplified Syntax:

Introduced in Java 10, the var keyword allows for type inference, letting the compiler determine the variable's type based on the assigned value. It enhances code readability and reduces verbosity.


      var message = "Hello, World!"; // Compiler infers String type
      var number = 42; // Compiler infers int type
    

Variable Naming Conventions

Best Practices:

Variable names should be descriptive and follow camelCase notation. They should start with a letter, underscore, or dollar sign, and can contain digits but not start with them. Avoid using reserved keywords.


      int studentAge = 20;
      double accountBalance = 1500.50;
    

Variable Scope

Understanding Scope:

The scope of a variable determines its visibility and lifetime within a program. Variables can be local, instance, or static, each with different scopes impacting their accessibility and duration.


      public class Example {
        static int classVariable = 10; // Class scope

        public void method() {
          int localVariable = 20; // Method scope
          System.out.println(localVariable);
        }
      }
    
logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025