The static keyword in Java is used primarily to administer memory. We can apply the static keyword to variables (also called class variables), methods (also called class methods), blocks, and classes that are iterated. The static keyword belongs to the class, not to the instance of the class.
Static variables can be used to reference common properties of all objects (which are not unique to each object), such as the employee's company name, student's school name, etc.
The static variable takes memory only once in the Class Area at load time.
Find out what happens when there are no static variables
Assuming there are 500 students in the university, now the instance of the data members will get the memory each time the object is created. All students have unique rollno and name so the instance of the data member is good. Here, college is the common property of all subjects. If we make it static, this field will only take memory once.
Note: The static attribute in Java is shared to all objects.
Example of static variables in Java
The output:
Counter program that does not use static variables
In our example, we create an instance variable named count that is incremented in the constructor. When this instance variable takes memory at the time of object creation, each object will have a copy of that instance variable, if it is incremented, it will not reflect the other objects. So each object will have a value of 1 in the count variable.
The output:
The output:
The output:
Another example of a static method that performs normal computation
The output:
The static method can not use non-static data members or call non-static methods directly.
This and super keywords can not be used in static.
The output:
Running the program will result in Compile Time Error.
Question: Why is the main method in Java is static?
Because the object is not necessary to call the static method if it is non-static, the first JVM creates the object and then calls the main () method, which can cause memory allocation problems remember side.
Question: Can we execute a program without the main () method?
Yes, one of those ways is the static block in previous versions of JDK, not in JDK 1.7.
To find out more about how to code if else in java:
Static keyword in Java
When you declare a variable to be static, that variable is called a static variable.Static variables can be used to reference common properties of all objects (which are not unique to each object), such as the employee's company name, student's school name, etc.
The static variable takes memory only once in the Class Area at load time.
Advantages of static variables
Static variables make your program memory more efficient (saving memory).Find out what happens when there are no static variables
Assuming there are 500 students in the university, now the instance of the data members will get the memory each time the object is created. All students have unique rollno and name so the instance of the data member is good. Here, college is the common property of all subjects. If we make it static, this field will only take memory once.
Note: The static attribute in Java is shared to all objects.
Example of static variables in Java
The output:
Counter program that does not use static variables
In our example, we create an instance variable named count that is incremented in the constructor. When this instance variable takes memory at the time of object creation, each object will have a copy of that instance variable, if it is incremented, it will not reflect the other objects. So each object will have a value of 1 in the count variable.
The output:
Counter program with static variables in Java
As you can see from the above, the static variable will take memory only once, if any object changes the value of the static variable, it will still remember its value.The output:
Static method in Java
If you apply the static keyword to any method, that method is called a static method.- A static method is a class, not a class object.
- A static method can be summoned without creating an instance of a class.
- The static method can access the static data member and can change its value.
The output:
Another example of a static method that performs normal computation
The output:
Some restrictions of static methods
There are two major limitations to the static method. That is:The static method can not use non-static data members or call non-static methods directly.
This and super keywords can not be used in static.
The output:
Running the program will result in Compile Time Error.
Question: Why is the main method in Java is static?
Because the object is not necessary to call the static method if it is non-static, the first JVM creates the object and then calls the main () method, which can cause memory allocation problems remember side.
The static block in Java
Used to initialize static data members. It is implemented before the main method at class load time. Here is an example of a static block in Java:Question: Can we execute a program without the main () method?
Yes, one of those ways is the static block in previous versions of JDK, not in JDK 1.7.
To find out more about how to code if else in java:
Comments
Post a Comment