1.
Java static method
If you apply static keyword with any method, it is known as static method.
o A static method belongs to the class rather than the object of a class.
o Static members are loaded into the memory at the time of class loading.
o Static members are stored in class or method area.
o A static method can be invoked without the need for creating an object.
o A static method can access static data member and can change the value of it.
Accessibility of static methods
• Static methods can be accessed directly inside a static methods even inside a main
method without creating an object.
• Static methods and variables can be accessed directly inside a non - static methods
within a same class.
• Static members can be accessed outside the class by using the class name followed by
( . ) dot operator.
Example :
class Calculate{
static int cube(int x){
return x*x*x;
}
public static void main(String args[]){
int result=[Link](5);
[Link](result);
}
}
Restrictions for the static method
There are two main restrictions for the static method. They are:
1. The static method can not use non static data member or call non-static method directly.
2. this and super cannot be used in static context.
Why is the Java main method static?
It is because the object is not required to call a static method. If it were a non-static
method, JVM creates an object first then call main() method that will lead the problem of
extra memory allocation.
Garbage collection in Java
Garbage collection in Java is the process by which the Java Virtual Machine (JVM)
automatically reclaims memory by destroying objects that are no longer reachable in the
program. The finalize() method and [Link]() method are two mechanisms associated
with garbage collection.
Note:
In Java we can’t delete the object but we can make an object eligible for garbage
collection by de-referring an object
finalize() Method
protected void finalise(){}
The finalize() method is called by the garbage collector on an object when garbage
collection determines that there are no more references to the object. This method allows
the object to clean up resources before it is collected.
finalise() method present in [Link] class which is used to clean up the process.
[Link]() Method
The [Link]() method is a request to the JVM to perform garbage collection. However,
calling [Link]() does not guarantee that the garbage collector will actually run. It’s
merely a suggestion to the JVM.
Example :
package [Link];
public class Demo {
@Override
protected void finalize() {
[Link](this + " has been garbage collected");
public static void main(String[] args) {
Demo d = new Demo();
Demo d1 = new Demo();
[Link](d + " " + d1);
d = d1;
d1 = null;
d = null;
[Link]();
try {
[Link](2000);
} catch (InterruptedException e) {
[Link]();
}
}
}
Explanation:
o Nullify References:
obj1 = null;
obj2 = null;
The references obj1 and obj2 are set to null, making the objects eligible for garbage
collection.
o Request Garbage Collection:
[Link]();
This line requests the JVM to run the garbage collector. Note that it is not guaranteed
that the garbage collector will run immediately or at all.
o [Link]():
[Link]().gc();
This line is another way to request garbage collection, similar to [Link]().
o finalize() Method:
@Override
protected void finalize() throws Throwable {
[Link](this + " is being garbage collected.");
}
The finalize() method is overridden in the MyClass class to print a message when
the object is about to be garbage collected. This method will be called by the garbage
collector before reclaiming the memory.
• Deprecation of finalize(): The finalize() method has been deprecated in Java 9 and is
not recommended for resource management. Instead, use try-with-resources or
explicit resource management methods.
• No Guarantee with [Link](): Calling [Link]() does not guarantee that the
garbage collector will run. It merely suggests that the JVM should make an effort to
reclaim memory.