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

Popular posts from this blog

Four Pro Tips for Placement

Master your Resume: Prepare one good resume and keep the required things only which are relevant to that interview.   So do not give same old resume for different positions to different companies. Prepare your resume according to the position you are applying for.     Do not include anything in the resume which you do not know . For example you know only C and Java programming language but you are including C, Java and Python language in resume. So if interviewer will ask any question from Python then you can not answer to that question. So always put the right and required things in your resume. Master One Programming Language:          Practice any one programming language thoroughly and be the    master in that programming language. That does not mean you will not learn other programming languages. But it is recommended for placement interview that becoming master in one programming language will make you confident in the i...

Best approach to Solve a Coding Question

In this article, we will discuss about steps of problem solving means how perfectly we can solve a coding question within limited time in any coding rounds of IT interviews. When it comes to getting a tech job, an applicant goes through several rounds of an interview. Typically, those rounds are categorized into the following.  Aptitude & Reasoning.  Coding Round  Technical Interview Round.  HR Round.  Along with these rounds there are some others round as well for screening the best candidates. Those are like GD (Group Discussion) round, Communication round, Management round etc. depending on the companies and their approach of screening candidates. Among those rounds, the most important and challenging one is the Coding round . In this round some coding problems are given to the candidates and a candidate needs to solve the problem by writing a good and optimized program with any programming language which is allowed by...