Static Properties

Introduction

A static property is a property that belongs to the class itself rather than to individual objects (instances) of the class. Like static methods, static properties are declared using the static keyword and are accessed directly through the class name.

Static properties are useful for storing data that should be shared across all objects of a class. Examples include application settings, configuration values, constants, counters, version numbers, and default values.

Unlike instance properties, every object does not get its own copy of a static property. Instead, all objects share the same static property through the class.

For automation engineers, static properties are commonly used to store base URLs, browser names, timeout values, API endpoints, framework versions, environment names, and other shared configuration values.

In this tutorial, you’ll learn how to create and use static properties in Node.js.


What are Static Properties?

A static property is a property that belongs to a class instead of an object created from that class.

It is shared by all instances of the class.


Why Use Static Properties?

Static properties help developers:

  • Store shared data.

  • Avoid duplicate values.

  • Improve code organization.

  • Manage application configuration.

  • Define constants.

  • Reduce memory usage.

  • Build reusable frameworks.


Syntax

class Company {

    static companyName =
        "Tech Solutions";

}

console.log(
    Company.companyName
);

Example 1: Basic Static Property

class School {

    static schoolName =
        "ABC Public School";

}

console.log(
    School.schoolName
);

Sample Output

ABC Public School

Example 2: Static Property Shared by All Objects

class Employee {

    static company =
        "OpenAI";

}

const emp1 =
    new Employee();

const emp2 =
    new Employee();

console.log(
    Employee.company
);

Sample Output

OpenAI

The company property belongs to the class and is shared by all employee objects.


Example 3: Static Counter

class User {

    static count = 0;

    constructor() {

        User.count++;

    }

}

new User();

new User();

new User();

console.log(
    User.count
);

Sample Output

3

Example 4: Static Configuration Value

class Config {

    static timeout =
        5000;

}

console.log(
    Config.timeout
);

Sample Output

5000

Example 5: Real-World Example

Application version.

class Application {

    static version =
        "1.0.0";

}

console.log(
    Application.version
);

Sample Output

1.0.0

Automation Testing Example

Static properties are commonly used in automation frameworks to store shared configuration values.

Playwright Example

Store the application URL.

class Config {

    static baseUrl =
        "https://example.com";

}

console.log(
    Config.baseUrl
);

Selenium Example

Store the browser name.

class BrowserConfig {

    static browser =
        "Chrome";

}

console.log(
    BrowserConfig.browser
);

Cypress Example

Store the environment.

class Environment {

    static name =
        "QA";

}

console.log(
    Environment.name
);

API Testing Example

Store the API base URL.

class ApiConfig {

    static baseUrl =
        "https://api.example.com";

}

console.log(
    ApiConfig.baseUrl
);

Data-Driven Testing Example

Store the CSV file path.

class TestData {

    static csvFile =
        "./data/users.csv";

}

console.log(
    TestData.csvFile
);

Static Properties vs Instance Properties

Feature Static Property Instance Property
Belongs to Class Object
Uses static keyword Yes No
Shared across all objects Yes No
Requires object creation No Yes
Accessed using Class name Object name
Common use Shared configuration Object-specific data

Common Mistakes

Accessing a Static Property Through an Object

Incorrect:

const config =
    new Config();

console.log(
    config.baseUrl
);

Correct:

console.log(
    Config.baseUrl
);

Storing Object-Specific Data as Static

Use instance properties for values that are unique to each object.


Modifying Shared Values Unnecessarily

Be careful when changing static properties because the change affects all objects that rely on the class.


Best Practices

  • Use static properties for shared data.

  • Store configuration values as static properties.

  • Keep constants in static properties.

  • Access static properties using the class name.

  • Avoid storing object-specific data in static properties.

  • Use meaningful property names.

  • Group related configuration values into dedicated classes.


Conclusion

Static properties provide a convenient way to store data that belongs to a class rather than individual objects. They are ideal for shared values such as configuration settings, constants, counters, version numbers, and application-wide information.

For automation engineers, static properties simplify framework design by centralizing commonly used values like base URLs, browser names, API endpoints, timeout settings, and environment configurations.

Understanding static properties will help you build cleaner, more organized, and more maintainable Node.js applications and automation frameworks.


Frequently Asked Questions (FAQs)

What is a static property?

A static property is a property that belongs to a class instead of an object.


How do you declare a static property?

Use the static keyword inside a class.


How do you access a static property?

Access it using the class name, for example, Config.baseUrl.


Are static properties shared by all objects?

Yes. All objects of the class share the same static property.


Why are static properties useful in automation testing?

They provide a central location for shared configuration values such as URLs, browser settings, API endpoints, timeouts, and environment names.


Key Takeaways

  • Static properties belong to the class.

  • They are declared using the static keyword.

  • Static properties are shared by all objects.

  • They are accessed using the class name.

  • They are ideal for configuration values and constants.

  • Static properties reduce duplicate data.

  • They improve framework organization.

  • Use instance properties for object-specific data.

  • Automation frameworks commonly use static properties for shared settings.

  • Static properties help build scalable and maintainable Node.js applications.