Skip to main content

Understanding Java: public static void main(String[] args)

 

Java is a popular and frequently used programming language because of its ease of use, adaptability, and platform independence. It was developed by James Gosling and his colleagues at Sun Microsystems, and it has since gained popularity as one of the most extensively used languages for creating a range of applications, including mobile and enterprise systems in addition to desktop and web applications.

The most common format for writing Java program is plain text files with the.java extension. The Java compiler (javac), a component of the Java Development Kit (JDK), is then used to compile the source code into bytecode. Because the JVM can run the bytecode, Java is a "write once, run anywhere" language.

To get a feel for Java's syntax and what exactly is public static void main(String[] args), let's now look at some fundamental coding ideas in Java further in this article!

The start of a Java code is public static void main(String[] args). It is a distinct method that acts as the program's execution's starting point.

Here's an example that demonstrates the usage of public static void main(String[] args):

public class Main {

    public static void main(String[] args) {

        System.out.println("Hello, world!");

    }

}

Public:

Public is an access modifier used in Java to specify a class, method, variable, or interface's visibility or accessibility. When a member is made public, anyone with access to the program or other programs can access it from anywhere.

Here is an illustration of how to use public with a class and how accessible it is:

public class MyClass {

    public void publicMethod() {

        System.out.println("This is a public method.");

    }

}

public class Main {

    public static void main(String[] args) {

        MyClass myObject = new MyClass();

        myObject.publicMethod();

    }

}

In this illustration, we have the class MyClass and the public function publicMethod(). A message is printed to the console by the publicMethod().

Another class that has a public static void main(String[] args) method is the class Main. We construct a MyClass instance inside the main function called myObject, and then we call the publicMethod() on this object.

In a nutshell, the public access modifier enables access to classes, methods, variables, or interfaces from any location inside the program or from within other programs. The highest level of accessibility is offered by it.

Static:

When defining a member (method or variable) that belongs to the class itself and not an instance of the class in Java, the keyword static is used. Without having to make a class object first, it can be accessed just using the class name.

An illustration of the use of static with a static method and a static variable is provided below:

public class MyClass {

    public static int staticVariable = 10;

    public static void staticMethod() {

        System.out.println("This is a static method.");

    }

}

public class Main {

    public static void main(String[] args) {

        System.out.println(MyClass.staticVariable);

        MyClass.staticMethod();

    }

}

The static variable staticVariable and the static method staticMethod() in this example are both members of the class MyClass. The static variable is set to 10 at initialization.

The public static void main(String[] args) method is a part of the Main class. The static variable staticVariable is directly accessed within the main method, and its value is printed using System.out.println. Additionally, we use the class name to directly invoke the static method staticMethod().

In a broader sense the static keyword in Java is used to specify class members (methods or variables) rather than class instances. Without making a class object, they can be accessed directly by using the class name.

Void:

The Java language uses the keyword void as a method return type. The return type of a method indicates whether or not it returns any value when it is void.

Here is an illustration of how to use void as a return type:

public class MyClass {

    public void voidMethod() {

        System.out.println("This is a void method.");

    }

}

public class Main {

    public static void main(String[] args) {

        MyClass myObject = new MyClass();

        myObject.voidMethod();

    }

}

In this illustration, the class MyClass has a method called voidMethod(). The voidMethod() method doesn't return anything and doesn't have a return type. Just a message is printed to the console.

The public static void main(String[] args) method is a part of the Main class. We construct a MyClass instance inside the main function called myObject, and then we call the voidMethod() on this object.

Java uses the void keyword as the return type for methods that don't return anything. It implies that the technique only serves to carry out a specific action or task and does not yield any results.

Main:

A particular method called main in the Java programming language is where a Java program starts to run. Every Java program must contain it, and it has the unique signature public static void main(String[] args).

Here's an example that demonstrates the usage of the main method:

public class Main {

    public static void main(String[] args) {

        System.out.println("Hello, world!");

    }

}

We have a class called Main in this illustration. The main() method inside the class has the following signature: public static void main(String[] args). The single statement that outputs "Hello, world!" to the console in the main method's body uses the System.out.println() method.

You can specify where your program's execution will begin and write the first line of code that will be run when the program is run using the main method. Keep in mind that the main method can also take an args parameter that is an array of strings. This enables you to run your program with command-line arguments.

Conclusion

The public static void main(String[] args) method's function and structure must be understood in order to create and run Java programs. To master any programming language, keep in mind that practice is essential. You have to keep learning, trying new things, and creating projects to improve your Java skills. So go in and start coding with Java to unleash its potential and open up a world of limitless programming possibilities.

 

 

 

 

Comments