Table of contents
1.
Introduction 
2.
Static methods 
3.
Static properties
4.
More examples
5.
Frequently Asked Questions
6.
Key Takeaways
Last Updated: Mar 27, 2024

Static Methods and Properties

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction 

Typescript is a strongly typed, object-oriented, compiled language. It is also a set of tools and typed superset of javascript that means it is Javascript with additional functions and features. And these blogs deal with static methods and properties in typescript. The class that contains static members can be accessed without creating an object by using the class name and dot notation.

For example -

<ClassName>.<StaticMember>

we use the keyword static for defining a static member in the class.

Example -

class Square {
    static side: number = 4;
}

In this example, class Square contains a static member side, which will be accessed by Square.side.

Following javascript code will be generated by the typescript for the above mentioned class -

var Square = /** @class */ (function () {
    function Square() {
    }
    Square.side = 4;
    return Square;
}());

Static methods 

The method that can be shared across class instances is called the static method. We use the static keyword for declaring the static method before the method name.

class Employee {
    private static headcount: number = 0;


    constructor(
        private Name: string,
        private lastName: string,
        private jobTitle: string) {


        Employee.headcount++;
    }


    public static getHeadcount() {
        return Employee.headcount;
    }
}

In this example, the access modifier of the declared static property headcount is changed from public to private. The headcount value doesn't change outside the class without creating a new employee object.

 

Furthermore, static method getHeadcount() is added inside the class, which returns the value of the headcount static property.

we use className.staticMethod() syntax to call the static method.

let mahinder = new Employee('Mahinder', 'singh', 'Front-end Developer');
let jugnu = new Employee('Jugnu', 'kaur', 'Back-end Developer');
let ram = new Employee('ram', 'kapoor', 'Back-end Developer');
console.log(Employee.getHeadcount)); // 3

Output- 

Static properties

The property that can be shared among all class instances is called static. we use static keywords to declare a static property. className.propertyName syntax is used to access the static property.

Example-

class Employee {
    static headcount: number = 0;


    constructor(
        private initialName: string,
        private lastName: string,
        private job: string) {


        Employee.headcount++;
    }
}

Here the static property headcount is initialized to 1, and whenever a new object is created headcount value is increased by 1.

now we are creating two objects to check the value of headocunt.

let mahinder = new Employee('Mahinder', 'singh', 'Front-end Developer');
let jugnu = new Employee('Jugnu', 'kaur', 'Back-end Developer');
let ram = new Employee('ram', 'kapoor', 'Back-end Developer');
console.log(Employee.headcount); // 3

This will return 3 as expected.

Output-

More examples

An example where a class contains a static member as well as a nonstatic member.

class Circle {
    static pi = 3.14;
    pi = 3.1;
}


Circle.pi; // returns 3.14


let circleObj = new Circle();
console.log(circleObj.pi); // returns 3.1

Output-

This example shows that static and non-static members can have the same name without showing any error. We use dot notation for accessing the static member, and for accessing the non-static member, we use the object.

LETS SEE ONE MORE EXAMPLE -

class Circle {
    static pi = 3.14;


    static calculateArea(radius:number) {
        return this.pi * radius * radius;
    }


    calculateCircumference(radius:number):number { 
        return 2 * Circle.pi * radius;
    }
}


console.log(Circle.calculateArea(5)); // returns 78.5

Output-

let circleObj = new Circle();
console.log(circleObj.calculateCircumference(5)); // returns 31.4000000

Output-

//circleObj.calculateArea(); <-- cannot call this

In this example, we have a class containing a static method that includes both static and non-static members, which are calculateArea and calculateCircumference. Here we access the static field pi with this.pi in the static method, and we use Circle.pi for accessing the non-static (instance) method.

Frequently Asked Questions

1.Does typescript have static classes?

No, typescript doesn't have static classes and there is no need for them. 

2.Can we access Static members by object instance?

Typescript does not allow a static property or method to be affected by an object instance, therefore, we cannot access the static members by the object instance.

Key Takeaways

In this blog, we learned about Static Methods and Properties. Don't come to a halt here. Check out our Typescript vs Javascript: Learn the difference | Coding Ninjas Blog. Can you also check out the TypeScript Interview Questions | Part 1 | Coding Ninjas Blog blog? Check out more blogs here. More Blogs.

Live masterclass