pls explain me static varaible in java not at all understanding it ??
hello aspirant
basically in java, Static variable is used for some common properties of the objects example- school name of students, college name etc.it is common to all and not at all unique.it is stored in static memory(heap memory). and can be called by object.
it makes program easy to understandable and effecient.it manages memory. In this a copy of variable is created and shared. it is among the class level so it is same for all the instamces of the class.These are globally acceptable.
Syntax of static variable is :- static data_type variable_name;
It is a very good memory management technique.
you can refer to the following example for better understanding and declaration
class Understand;
{ static string name;
static int address;
static int age;
//static method
static void disp(){
System.out.println("name is:"+name);
System.out.println("address is:"+address);
System.out.println("age is:"+age);
}
// static method
public static void main(String []args)
{ name= "miss";
address= street no 2, ghaziabad;
age= 40;
disp();
}
}
hope you understand
Hi,
The static variable is used to refer to the common property of all objects
for example: The company name of employees, college name of students etc. Static variables gets memory only once in the class area at the time of class loading. Using a static variable makes your program more memory efficient. Static variable belongs to the class rather than the object.
Hope this information is helpful
Thankyou.
Hello, hope you are doing well!
Static Variables in Java-
-
Class variables also known as static variables are declared with the static keyword in a class, but outside a method, constructor or a block.
-
There would only be one copy of each class variable per class, regardless of how many objects are created from it.
-
Static variables are rarely used other than being declared as constants. Constants are variables that are declared as public/private, final, and static. Constant variables never change from their initial value.
-
Static variables are stored in the static memory. It is rare to use static variables other than declared final and used as either public or private constants.
-
Static variables are created when the program starts and destroyed when the program stops.
-
Visibility is similar to instance variables. However, most static variables are declared public since they must be available for users of the class.
-
Default values are same as instance variables. For numbers, the default value is 0; for Booleans, it is false; and for object references, it is null. Values can be assigned during the declaration or within the constructor. Additionally, values can be assigned in special static initializer blocks.
-
Static variables can be accessed by calling with the class name ClassName.VariableName.
-
When declaring class variables as public static final, then variable names (constants) are all in upper case. If the static variables are not public and final, the naming syntax is the same as instance and local variables.