TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

JavaScript OOPs Static Method

JavaScript OOPs Static Method with javascript tutorial, introduction, javascript oops, application of javascript, loop, variable, data types, operators, javascript if, switch, operators, objects, form validation, map, typedarray etc.

<< Back to JAVASCRIPT

JavaScript static Method

The JavaScript provides static methods that belong to the class instead of an instance of that class. So, an instance is not required to call the static method. These methods are called directly on the class itself.

Points to remember

  • The static keyword is used to declare a static method.
  • The static method can be of any name.
  • A class can contain more than one static method.
  • If we declare more than one static method with a similar name, the JavaScript always invokes the last one.
  • The static method can be used to create utility functions.
  • We can use this keyword to call a static method within another static method.
  • We cannot use this keyword directly to call a static method within the non-static method. In such case, we can call the static method either using the class name or as the property of the constructor.

JavaScript static Method Example 1

Let's see a simple example of a static method.

<script>
class Test
{
  static display()
  {
    return "static method is invoked"
  }
}
document.writeln(Test.display());
</script>
Test it Now

Output:

static method is invoked

Example 2

Le's see an example to invoke more than one static method.

<script>
class Test
{
  static display1()
  {
    return "static method is invoked"
  }
  static display2()
  {
    return "static method is invoked again"
  }
}
document.writeln(Test.display1()+"<br>");
document.writeln(Test.display2());
</script>
Test it Now

Output:

static method is invoked
static method is invoked again

Example 3

Let's see an example to invoke more than one static method with similar names.

<script>
class Test
{
  static display()
  {
    return "static method is invoked"
  }
  static display()
  {
    return "static method is invoked again"
  }
}
document.writeln(Test.display());
</script>
Test it Now

Output:

static method is invoked again

Example 4

Let's see an example to invoke a static method within the constructor.

<script>
class Test {
  constructor() {
  document.writeln(Test.display()+"<br>"); 
  document.writeln(this.constructor.display()); 
  }

  static display() {
      return "static method is invoked"
  }
}
var t=new Test();
</script>
Test it Now

Output:

static method is invoked
static method is invoked 

Example 5

Let's see an example to invoke a static method within the non-static method.

<script>
class Test {
  static display() {
      return "static method is invoked"
  }
  
 show() {
  document.writeln(Test.display()+"<br>"); 
  }  
}
var t=new Test();
t.show();
</script>
Test it Now

Output:

static method is invoked
Next TopicJS Encapsulation




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf