What Kinds of Variables Can a Class Consist Of? - Java basics

A class in Java can consist primarily of three types of variables: instance variables, static (class) variables, and local variables.

Instance variables store object-specific data, static variables store common data shared by all objects, and local variables exist only inside methods.

Instance Variables

Declared inside the class but outside methods.

Advertisement

Each object gets its own copy.

Used when object-specific data is required (e.g., each user object has its own username and password).

Static (Class) Variables

Declared with the static keyword, shared across all objects, with memory allocated only once (e.g., a common wait time, environment URL, or driver path).

Local Variables

Declared inside methods or constructors, existing only during method execution.

They have no default values and must be initialized before use (e.g., temporary counters, flags, or dynamic data inside methods).

Real-life example

Think of a BankAccount template. Each account has a different balance (instance variable), all accounts belong to the same bank name (static variable), and any temporary value you calculate inside a method, like interest for this month, is a local variable.

Real-time testing example

In a Selenium Page Object class, web elements declared with @FindBy are instance variables, a global timeout defined once is a static variable, and a temporary variable used only inside a test method is a local variable.

Project example (STAR)

Situation: In my framework, we had to design reusable Page Objects.

Task: Define variables so objects remain independent while certain values stay common.

Action: I used instance variables for element locators, static variables for constants such as wait time and the base URL, and local variables for temporary values within methods.

Result: The Page Object classes became more readable, memory-efficient, and easy to maintain across the automation suite.

Quick summary

Instance = per object, Static = shared, Local = method-level.

These are used heavily in frameworks (instance for elements, static for constants, local for temporary data), helping maintain a clean, optimized, and scalable Java + Selenium architecture.


What is a Singleton Class?

A Singleton class is a design pattern in which only one object of the class is allowed to be created during the entire execution of the application. It provides a single shared instance accessible globally.

Key points

  • The constructor is private, so nobody can create objects from outside.
  • The class holds a static reference to itself.
  • A public static method (usually getInstance()) returns the single instance.

Why it's useful in frameworks

  • Ensures one WebDriver instance per test run, preventing multiple browsers from opening.
  • Maintains a single configuration manager, logging utility, or database connection across the project.
  • Helps maintain consistent test execution while reducing memory usage.

Real-life example

Think of a country's Prime Minister—there can be only one at a time, and everyone refers to the same authority. That is exactly how a Singleton object works.

Real-time testing example

In Selenium frameworks, the WebDriver Manager or DriverFactory is often implemented as a Singleton so the same browser driver instance is reused across tests, avoiding "Session not created" errors or multiple browser conflicts.

Project example (STAR)

Situation: While building a hybrid Selenium framework, multiple browser instances were opening accidentally.

Task: Ensure the framework uses only one WebDriver instance throughout the run.

Action: I implemented WebDriver initialization inside a Singleton DriverManager class with a private constructor and a public getInstance() method.

Result: Browser conflicts were resolved, execution stabilized, and the framework became easier to maintain.


The Three Steps for Creating an Object

An object is created from a class using the new keyword in three steps:

Declaration

A variable declaration with a variable name and an object type (declare a reference variable).

Instantiation

The new keyword is used to create the object.

Initialization

The new keyword is followed by a call to a constructor, which initializes the new object by connecting the reference to the object.

Real-life example

Think of booking a cab.

You create a contact name (reference), the cab company assigns a driver and car (object creation), and the driver is linked to your booking (assignment).

Real-time testing example

In Selenium, declare a WebDriver reference (WebDriver driver;), create the object (new ChromeDriver();), and assign it (driver = new ChromeDriver();).


When is the byte Datatype Used?

The byte data type is a primitive, 8-bit signed integer with a value range of −128 to 127 (default value is 0).

It is mainly used to save memory in large arrays where memory savings matter most and can also be used in place of int when the value range is small.

When to use byte

  • Memory efficiency — For large datasets (millions of values), using byte instead of int saves significant memory.
  • Binary or raw data — Low-level I/O such as reading binary files, processing image/audio bytes, and communicating with hardware or network streams.
  • Small value range — When a number will always stay within −128 to 127.
  • Serialization and data transmission — File systems, protocols, and APIs often work in bytes.

Real-life example

Think of packing clothes for travel. With limited luggage space, you carry only small, light items. Similarly, byte is used when space is limited and data values are small.

Real-time testing example

In automation, byte is useful when reading binary files, image data, or HTTP response bytes—for example, when validating image download size or file-upload content.

byte[] arrays are commonly used when testing file uploads/downloads and API response streams.

Project example (STAR)

Situation: During API testing, I had to verify that downloaded images were not corrupted.

Task: Read the file content with the smallest memory footprint.

Action: I used a byte array to read the image data and validated the file size and checksum.

Result: The test became faster, memory usage decreased, and it provided accurate binary-level validation.


while vs do-while Loop

Both loops execute a block of code multiple times, but they differ in when the condition is checked.

while loop

Checks the condition before executing the block.

If the condition is false from the outset, the code block may not execute at all.

do-while loop

Executes the code block once before checking the condition.

This means the block always executes at least once, regardless of whether the condition is true or false.

 
// while loop

while (condition) {

   // Code to execute

}

// do-while loop

do {

   // Code to execute

} while (condition);
 

How to remember it

Use a while loop when you want to check the condition first.

Use a do-while loop when you want the code to run at least once and check the condition afterward.


FAQs

1. What kinds of variables can a Java class have?

Instance variables (object-specific), static/class variables (shared across all objects), and local variables (temporary, inside methods).


2. What is the difference between instance and static variables?

Each object gets its own copy of an instance variable, whereas a static variable is shared across all objects and is allocated only once.


3. What is a Singleton class?

A design pattern that allows only one object of a class during the whole application using a private constructor, a static self-reference, and a public getInstance() method.

It is commonly used for WebDriver, configuration readers, and log managers.


4. What are the three steps to create an object in Java?

  • Declaration (reference variable)
  • Instantiation (new keyword)
  • Initialization (constructor call that connects the reference to the object)

5. When is the byte data type used?

When storing small integers (−128 to 127) and memory efficiency matters, such as in large arrays, binary/file processing, streams, and network or API byte data.


6. What is the difference between while and do-while?

A while loop checks the condition before executing the code (it may run zero times), whereas a do-while loop executes the code once before checking the condition (it always runs at least once).