Static Member
The term static refers to memory.
The static memory once allocated for an object will be used for all other objects.
It occupies more memory
The static members are declared by using the keyword static and they are accessible by using class name.
Non Static Member
It uses dynamic memory.
The dynamic memory is allocated for every object individually. Hence it is more secure, and occupies less memory.
The non static members are accessible with in the class by using "this" keyword.
Example
class Demo1 {
public n:number=0;
public static s:number=0;
constructor()
{
this.n+1;
Demo1.s++;
}
public print():void {
console.log("s=" + Demo1.s);
console.log("n=" + this.n);
}
}
let obj1 =new Demo1();
obj1.print();
let obj2=new Demo1();
obj2.print();
let obj3=new Demo1();
obj3.print();
Output
s=1
n=0
s=2
n=0
s=3
n=0
Static Keyword /Access Specifier
-------------------------------
*static keyword can be applied to a field (or) method
1.static field
2.static method
static field:
-------------
*the field attached to a class is called "static field"
*static field will be allocated with memory only single time, it will be
shared by all the objects of a class
*static field can be referenced only with class name, not with object variable
static method:
--------------
*the method attached to a class is called "static method"
*static method can access only static fields
*static method can be referenced only with class name, not with object variable
No comments:
Post a Comment
Thank you for visiting my blog