JavaScript – Static Method


What is a static method ?

This is a method which directly belongs to the class instead of instance of the class.

Therefore an instance is not required to call this static methods.

Mostly these static methods are called directly inside the class.

How to create static method ?

“static” keyword is used to declare a static method.

A class can contain one or more static methods.

Multiple static methods with the same names can be declared, But javascript executes the last defined one.

Static method names can be of any name.

How static methods are used ?

Static methods are used to create common utility functions.

A static method can be called from another static method by using the “this” keyword.

How static methods can be called from non-static methods ?

We cannot use “this” keyword to call static methods within non-static methods.

However, by using class or constructor property we can call static methods from non-static method.


Training javascript !!!

Click to Learn More about – Javascript online learning

Syntax :

static methodName (argument1,argument2,argument3...argumentN);

1. Simple static method.

Example :

		<script>
                        class StudentInfo{
                                static Marks(english,tamil,science,maths,history,geography){
                                        document.writeln("<b>Student Mark Info</b><br/>");
                                        document.writeln("English Marks = " + english+"<br/>");
                                        document.writeln("Tamil Marks = " + tamil+"<br/>");
                                        document.writeln("Science Marks = " + science+"<br/>");
                                        document.writeln("Maths Marks = " + maths+"<br/>");
                                        document.writeln("History Marks = " + history+"<br/>");
                                        document.writeln("Geography Marks = " + geography+"<br/><br/>");
                                }
                        }
                        StudentInfo.Marks("100","100","99","100","89","80");
                        StudentInfo.Marks("90","80","79","67","98","81");
                        StudentInfo.Marks("99","100","97","93","90","38");
                </script>

Output :

2. Multiple static methods.

Example :

<script>
                        class StudentInfo{
                                static BasicInfo(name,age,qualification){
                                        document.writeln("<b>Student Info</b><br/>");
                                        document.writeln("Name = " + name+"<br/>");
                                        document.writeln("Age = " + age+"<br/>");
                                        document.writeln("Qualification = " + qualification+"<br/>");
                                }
                                static Marks(english,tamil,science,maths,history,geography){
                                        document.writeln("<b>Student Mark Info</b><br/>");
                                        document.writeln("English Marks = " + english+"<br/>");
                                        document.writeln("Tamil Marks = " + tamil+"<br/>");
                                        document.writeln("Science Marks = " + science+"<br/>");
                                        document.writeln("Maths Marks = " + maths+"<br/>");
                                        document.writeln("History Marks = " + history+"<br/>");
                                        document.writeln("Geography Marks = " + geography+"<br/><br/>");
                                }
                        }
                        StudentInfo.BasicInfo("Abdul","14","10th");
                        StudentInfo.Marks("100","100","99","100","89","80");
                        StudentInfo.BasicInfo("Richard","13","9th");
                        StudentInfo.Marks("90","80","79","67","98","81");
                        StudentInfo.BasicInfo("SivaRam","14","10th");
                        StudentInfo.Marks("99","100","97","93","90","38");
                </script>

Output :


3. Multiple static methods with same names.

Example :

 <script>
                        class StudentInfo{
                                static Marks(english,tamil,science,maths,history,geography){
                                        document.writeln("<b>Student Mark Info</b><br/>");
                                        document.writeln("English Marks = " + english+"<br/>");
                                        document.writeln("Tamil Marks = " + tamil+"<br/>");
                                        document.writeln("Science Marks = " + science+"<br/>");
                                        document.writeln("Maths Marks = " + maths+"<br/>");
                                        document.writeln("History Marks = " + history+"<br/>");
                                        document.writeln("Geography Marks = " + geography+"<br/><br/>");
                                }

                                static Marks(english,tamil,science,maths,history,geography){
                                        document.writeln("<b>Latest Static method is executed in Javascript</b><br/>");
                                }

                        }
                        StudentInfo.Marks("100","100","99","100","89","80");
                        StudentInfo.Marks("90","80","79","67","98","81");
                        StudentInfo.Marks("99","100","97","93","90","38");
                </script>

Output :

4. Static method called inside the constructor.

Example :

<script>
                        class StudentInfo{
                                constructor(){
                                        StudentInfo.Marks("100","100","99","100","89","80");
                                        this.constructor.Marks("99","90","99","70","79","82");
                                }
                                static Marks(english,tamil,science,maths,history,geography){
                                        document.writeln("<b>Student Mark Info</b><br/>");
                                        document.writeln("English Marks = " + english+"<br/>");
                                        document.writeln("Tamil Marks = " + tamil+"<br/>");
                                        document.writeln("Science Marks = " + science+"<br/>");
                                        document.writeln("Maths Marks = " + maths+"<br/>");
                                        document.writeln("History Marks = " + history+"<br/>");
                                        document.writeln("Geography Marks = " + geography+"<br/><br/>");
                                }
                        }
                        var a = new StudentInfo();
                </script>

Output :

5. Static method called from non-static methods.

Example :

<script>
                        class StudentInfo{
                                static Marks(english,tamil,science,maths,history,geography){
                                        document.writeln("<b>Student Mark Info</b><br/>");
                                        document.writeln("English Marks = " + english+"<br/>");
                                        document.writeln("Tamil Marks = " + tamil+"<br/>");
                                        document.writeln("Science Marks = " + science+"<br/>");
                                        document.writeln("Maths Marks = " + maths+"<br/>");
                                        document.writeln("History Marks = " + history+"<br/>");
                                        document.writeln("Geography Marks = " + geography+"<br/><br/>");
                                }
                                basicInfo(){
                                        document.writeln("<b>Student BasicInfo</b><br/>");
                                        document.writeln("Name = Abdul<br/>");
                                        StudentInfo.Marks("100","100","99","100","89","80");
                                }
                        }
                        var a = new StudentInfo();
                        a.basicInfo();
                </script>

Output :