0
What is static??
5 odpowiedzi
+ 6
It is a keyword just like a access specifier (in the class level ).
It means if u declare a variable or method as static it becomes a class variable or class method . That is only one copy of the variable is made and the value is same for all objects . If u alter the value in one object it will also be changed for other objects.
When u make a method as static it can be directly accessed by the class name .
eg:- class abc
{
static int a=2;
static void sum ()
{
a++;
}
public static void main ()
{
a++;
abc.sum ();//called using class name
System.out.println (a);// 4 will be printed
}
}
+ 1
A static variable is variable initialized one time, on the first time it's evaluated, then, it isn't initialized :
class A{
public :
static int a=0;
A(){
a++;
cout<<a;
}
};
int main (){
A obj1;
A obj2;
A obj3;
}
// Outputs :
123
Ask if you want more informations
0
Oh, looks like I was toi slow.. 😁
Excuse me !! I coded in C++...
0
me too i started typing when the qstn was just posted and when i finished it was well over 4 mins 😂😂😂
0
But you coded in Java 😂