The line “public static void” in java is very important to understand because it is the core and basic concept in java. We will understand each modifier separately one by one.
First of all I would like to say that it is not mandatory to get all the methods in java started with these modifiers “public static void”. It is optional we can use these modifiers as per based on our requirement.
First we will discuss the “public” modifiers, what happens when we use them in class , method and fields.
•Use ‘public’ modifier with a class: when we use ‘public’ modifier before a class that means this is a public class , we can access this class everywhere in java (i.e. Accessibility of the public class in the same package as well as in the different package). There is no privacy or no restrictions on public class. It is visible everywhere.
Syntax: public classname{}
•Use ‘public’ modifier with a method: when we use ‘public’ modifier before a method that means this is a public method, we can access this method everywhere in java (i.e. Accessibility of the public method in the same class and different classes of the same package as well as of the different packages). There is no privacy or no restrictions on public method. It is visible everywhere.
Syntax: public return-type methodname(){}
•Use ‘public’ modifier with a field: when we use ‘public’ modifier before a field that means this is a public variable, we can access this variable everywhere in java (i.e. Accessibility of the public fields in the same class and different classes of the same package as well as of the different packages). There is no privacy or no restrictions on public method. It is visible everywhere.
Syntax: public data-type field-name;
For Example: Java program is to demonstrate the behaviour of ‘public’ modifiers
class Demo{
// Declares Field x as public
public int x = 10;
// Declares Method as public
public void display(){
System.out.println("We are in Demo class");
}
}
// Declare class as public that access
// public field (x) and public method
// (display) of another class Demo
public class PublicClass{
public static void main(String args[]) {
Demo d = new Demo();
System.out.println("x = " + d.x);
d.display();
}
}
Output:
x = 10
We are in Demo class
Explanation: In the above example , there is a public field named ‘x’ and a public method named ‘display’ defined in a class named ‘Demo’, Now, we are accessing public fields and methods in a different class named ‘PublicClass’.
Second we will discuss the “static” modifiers, what happens when we use them in class , method and fields.
•Use ‘static’ modifier with a class: when we use ‘static’ modifier before a class that means this is a ‘static’ class. Inner class can only be static and Inner class static class objects can’t be created without using an Outer class because inner static class can access properties of outer class without referring to it.
Syntax: static class class-name{}
•Use ‘static’ modifier with a method: when we use ‘static’ modifier before a method that means this is a static method, we can also access static method without creating an object of the class (i.e. we can access directly with class-name). For static methods we don’t need to create an object of the class. When we access a static method with the class object then also it works fine it will not throw any error or exception.
Syntax: static return-type methodname(){}
•Use ‘static’ modifier with a field: when we use ‘static’ modifier before a field that means this is a static field, we can also access static fields without creating an object of the class (i.e. we can access directly with class-name). For static fields we don’t need to create an object of the class. When we access a static field with the class object then also it works fine it will not throw any error or exception.
Syntax: static data-type field-name;
For Example: Java program is to demonstrate the behaviour of ‘static’ modifiers
public class Demo{
// Declares Field x as static
public static int x = 10;
// Declares Method as static
public static void display(){
System.out.println("We are in static method");
}
public static void main(String args[]) {
Demo d = new Demo();
// Access static field directly, with class name
// and with an instance
System.out.println("x = " + x);
System.out.println("Demo.x = " + Demo.x);
System.out.println("d.x = " + d.x);
System.out.println();
// Call static method directly, with class name
// and with an instance
display();
Demo.display();
d.display();
}
}
Output:
x = 10
Demo.x = 10
d.x = 10
We are in static method
We are in static method
We are in static method
Third we will discuss the “void” what happens when we use them in method.
Use ‘void’ with a method: when we use ‘void’ before a method that means it processes but it does not return anything as a result.