I have a doubt regarding static methods. In the program written below, output will be: main
. I understand this because main
is a static method, so when class loads, it executes. If so, the same principle should apply for met()
also, right? As it is also static. Why does only main
executes whereas met()
doesn't when the class loads?
public class Test {
static void met() {
System.out.println("method");
}
public static void main(String[] args) {
System.out.println("main");
}
}
Static variable in Java is variable which belongs to the class and initialized only once at the start of the execution. It is a variable which belongs to the class and not to object(instance ). Static variables are initialized only once, at the start of the execution.
Static Block in Java It executes whenever the class is loaded in memory. One class can have numerous static blocks, which will be executed in the same sequence in which they are written.
In Java, static variables are also called class variables. That is, they belong to a class and not a particular instance. As a result, class initialization will initialize static variables. In contrast, a class's instance will initialize the instance variables (non-static variables).
Can we overload static methods? The answer is 'Yes'. We can have two or more static methods with the same name, but differences in input parameters.
No, this isn't correct.
Neither of these methods are called when the class is loaded.
main()
is called when you execute the class Test
.
Only static initialisers are called when the class is loaded. A static initialiser looks like this:
static
{
//code here
}
A class is loaded before the main()
method is executed, and therefore its static initialiser is run before the main()
method. The following snippet will make that clear.
public class TestA
{
static
{
System.out.println( "hello" );
}
public static void main( String[] args )
{
System.out.println( "bye" );
}
}
Types of methods There are basically two types of methods,
Instance Methods Belong to objects and you will always need an object/instance to call such methods.
static methods are the class methods and they can be called directly by class name, there is no need to have an instance of class to call them.
For example,
class Demo{
public void sayHello(){
System.out.println("Hello");
}
public static void sayHi(){
System.out.println("Hi")
}
public static void main(String args[]){
Demo.sayHi(); //Call to static/class method
Demo.sayHello(); //It will not work
Demo d = new Demo();
d.sayHello(); //It will work.
}
}
**But NONE of them gets called automatically when class loads.**
Main Difference between the two
In memory there is ONLY ONE copy of static methods which will be available for all the objects. But whenever an object is created a new copy of instance method is created for the object, so each object has its own instance method. Similar to instance & class variables.
Static methods are not meant to be run automatically, instead they are shared by all objects. Why main()
method is called, because it is the entry point of the program.
Apart from them, there is static block which is called automatically only once when the class is loaded.
Example
class Main{
static{
System.out.println("static block");
}
public static void main(String args[]){
System.out.println("main");
}
}
Output will be
static block
main
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With