what is a static class code example

Example 1: js class static function

class myClass{
 
constructor(){
this.myLocaleVariable=1;//this.varname in the constructer function makes a locale var
}
localfunction(){
return "im local unique to this variable";
}
static publicfunction(){
return "i can be called without an obj"
}
  
}
myClass.myPublicVariable = 0;



myClass.localfunction();//error
myClass.publicfunction();//"i can be called without an obj"
myClass.myLocaleVariable;//error
myClass.myPublicVariable;//0
var obj = new myClass;
obj.localfunction();//"im local unique to this variable"
obj.publicfunction();//error
obj.myLocaleVariable;//1
obj.myPublicVariable;//error

Example 2: what is static method

A static method belongs to the class rather than the object.
There is no need to create the object to call the static methods.
A static method can access and change the value of the static variable

Tags:

Java Example