Skip to Content
bytegarden

Variables

Java’s pass-by-value approach is designed for simplicity and consistency. By always passing a copy (either of the value or the reference), Java avoids the complexities of pass-by-reference, which can lead to unexpected behaviors in some cases. Java is not pass-by-reference

Variable Naming Rules

  • Variable names are case-sensitive.

  • Must begin with a letter (A-Z or a-z), dollar sign ($), or underscore (_).

  • Subsequent characters can be letters, digits (0-9), dollar signs, or underscores.

  • Cannot be a reserved keyword.

  • Should be meaningful and self-descriptive.

  • Examples:

    int age;
    double $salary;
    String _name;
    boolean isValid;

Final Keyword

Purpose of Final

  • Create immutable values (constants)

  • Prevent method overriding in inheritance

  • Stop class inheritance

  • Ensure design integrity

  • Enhance security in APIs

  • To understand better, see this code (StaticFinal.java, below)

    StaticFinal.java
    class Example {
        // Static variable (shared across all instances)
        static int staticCounter = 0;
    
        // Static initialization block
        static {
            System.out.println("Static block executed when class is loaded");
        }
    
        // Final variable (constant, cannot be changed)
        final int finalId;
    
        // Static final variable (constant shared by all instances)
        static final String COMPANY_NAME = "Tech Corp";
    
        Example(int id) {
            this.finalId = id;
            staticCounter++;
        }
    
        static void displayStaticInfo() {
            System.out.println("Company: " + COMPANY_NAME);
            System.out.println("Total objects created: " + staticCounter);
        }
    
        final void displayFinalInfo() {
            System.out.println("Object ID: " + finalId);
            // finalId = 200; // Would cause error - final variables can't be modified
        }
    }
    
    final class FinalClass {
        void showMessage() {
            System.out.println("This is a final class");
        }
    }
    
    // This would cause error:
    // class ChildClass extends FinalClass {} // Can't inherit from final class
    
    public class StaticFinal {
        public static void main(String[] args) {
            // Static members can be accessed without instance
            System.out.println("Initial count: " + Example.staticCounter);
    
            Example obj1 = new Example(101);
            Example obj2 = new Example(102);
    
            // Static method called on class
            Example.displayStaticInfo();
    
            // Final methods called on instances
            obj1.displayFinalInfo();
            obj2.displayFinalInfo();
    
            // Demonstrate static variable sharing
            System.out.println("Both objects share counter: " + Example.staticCounter); // Shows 2
    
            FinalClass finalObj = new FinalClass();
            finalObj.showMessage();
        }
    }
    Runs in your browser with Java 21. Ctrl/⌘ + Enter to run.Built with CheerpJ

Characteristics of Final

  • Must be initialized at declaration or in constructor
  • Value cannot be changed after initialization
  • Final classes cannot be extended
  • Final methods cannot be overridden
  • Compiler optimizes final code for better performance

Usage for Final

  1. Variables: Once assigned, their value cannot be changed

    final double PI = 3.14;   // Constant
    final String NAME;        // Must initialize in constructor
  2. Methods: Cannot be overridden by subclasses

    final void display() { }   // Cannot be overridden
  3. Classes: Cannot be subclassed

    final class Secure { }  // Cannot be inherited

Static Keyword

Purpose of Static

  • Declares members that belong to the class itself, not to instances
  • Creates shared resources across all instances
  • Enables utility functionality without object creation
  • For example, static variables can be accessed by using useColor(Color.BLACK) instead of using Color myColorBlack = new Color(). To understand better, see this code (StaticFinal.java, above)

Characteristics of Static

  • Belongs to class, not instances
  • Can be accessed without object creation
  • Memory efficient (single copy shared by all instances)
  • Cannot access non-static members directly

Usage for Static

  1. Variables: Shared among all instances of a class

    static int counter = 0;           // Shared across instances
    static final double PI = 3.14;    // Constant
  2. Methods: Can be called without creating an instance of the class

    static void utility() { }  // Called without instance
  3. Blocks: Used for static initialization of a class

    static {      // Runs when class loads
      // initialization code
    }
  4. Nested Class: Classes that do not have a reference to an instance of the outer class

    static class InnerHelper { }