public static void main (string args[]) Explanation

In Java, JVM (Java Virtual Machine) will always look for a specific method signature to start running an application, and that would be the public static void main (String args[]). The main() method represents the entry point of Java programs, and knowing how to use it correctly is very important.

In the above application example, we are using the public static void main. Each word has a different meaning and purpose. Keep learning to know more about each and every keyword.

It is an Access Modifier , which defines who can access this Method. Public means that this Method will be accessible by any Class(If other Classes can access this Class.).

Static is a keyword that identifies the class-related thing. It means the given Method or variable is not instance-related but Class related. It can be accessed without creating the instance of a Class.

It is used to define the Return Type of the Method. It defines what the method can return. Void means the Method will not return any value.

Main is the name of the Method . This Method name is searched by JVM as a starting point for an application with a particular signature only.

String args[] / String… args

Here args is an argument of the type String array. Which is used to pass the command-line argument to the main method. Though the type of the argument (String array) is fixed, you can still change the name from args to anything. Also with the introduction of java args, instead of writing String args[], String… args can be used.  You can either use String array (String args[]) or var args(String… args) variable of String type. Both will work the same way.

Given all the options, the syntaxes of the main methods are the same and all will work fine.

78 Comments → public static void main (string args[]) Explanation

Hi, Good explanation! If I have a Main class (Main.java) and two more classes, Schoolmember and Staff (Schoolmember.java and Staff.java), do I have to have “public static void main(String[] args)” in all of these or only in Main? Thank You

Sorry for the late reply. It depends on what you want to achieve. Main is supposed to work as starting point of the application. If you are using other classes in Main.java and trying to invoke those methods from Main.java then one main method in Main.java is enough.

But if these classes are independent and you want to execute them individually then they need to have main method too.

i found it nice and easy to understand the topic of the public static void main . It would be better to explain it using video . Than it gates to our mind and if we want to tack it out also it will not come .

Thank you, Kaushal. Creating a video for this short topic is not easy but will see if it is possible.

Well explained. Thank you

hi public is not access modifier , it is access specifier in java

What does it mean?

Thanks a lot. Explained in an easy way!!

Hi, I already knew the main method is the entry point of the program and makes it execute the program properly but this explanation is really good for adults and kids. Thank you very much. This helped me a lot for public static void main(String[] args). I appreciated it a lot. I am a seven year old kid and I love programming.

Welcome Sachet

you are wrong in main definition like “ss” there we are use “is”

Static Static ss a keyword….. what is the ss means

That was a typo. I meant to say “Static is a keyword….”. It has been corrected now.

A Java program is a sequence of Java instructions that are executed in a certain order. Since the Java instructions are executed in a certain order, a Java program has a start and an end.

Nice explanation….

What is the meaning of creating an instance of the class ??

What is the meaning of creating an instance of the class ?? Please reply me.

Nice One Explanation !!

thank u for the given content\

Main method is very important in java. Program execution starts from main () method only.

why we are using string[] args in pubic static void main(String[] args) why we are not using character array or int array i need elabrate answer

Sir how to call a method how the whole process goes in a program please explain with example

hello sir if we not use static in with voidmain() how we run void main() function without static keyword

You can write main method without static keyword, there will not be any issue. But it will not work as expected. It will be treated as normal method and in order to execute it you need to call given method explicitly. While static keyword will make this method to be treated as entry point. Hope it clarifies.

thank u for update ur information i have some cofusion in null point exception please send me a link of ur site in null point exception

I dont have any article specifically written on null pointer exception

Can you please explain…how internally works these SYSOUT statement?? System.out.println(“args[” +i+ “]:”+args[i] ); System.out.println(“args[ i ]:”+args[i] );

http://www.mathcs.emory.edu/~cheung/Courses/170/Syllabus/09/command-args.html

1) System -is a class in the java.lang package .

2) out -is a static member of the System class, and is an instance of java.io.PrintStream .

3) println -is a method of java.io.PrintStream. This method is overloaded to print message to output destination, which is typically a console or file.

what is command line argument

Command passed at time of runtime.. On console you pass your input as an argument..

what is array of string

It is a argument passed to Main method.

why we pass only string as the parameter of the main method?

Argument passed in main method is array of string and not string. This is the standard followed.

I want list and description of in build classes and their sub classes…like printsteam is class and println() is its method…

Why we usethis method main(string args())

It works as starting point of application execution.

Thanks I guess this will useful for me

hi can you explain why public static void should write in the same order/sequence as written now.

Public and static keyword position can be changed. There is no such rule of using public before static. But as void is method return type it should come just before method name which is main in this case. Hence this sequence. public static void main static public void main.

Both are same. But as a standard we right as in the first case.

The grammar in this article and in the comments is horrendous.

u write a new english dictionary

It is shortcut English grammar that is enough for developers to understand each other so they can write efficient English with minimal words – like good programming style that is less verbose

I want to know why JVM treats main(String [] args) method as a starting point of program, can anyone tell me the internal functioning of this method, like how it gets execute and what threads are required to execute this method so that JVM can understand this method as the starting point of the program.?

Suppose you are watching a movie and the scene of movie starts from middle of a story and as you continue watching movie, u need to understand the current scene of movie and for that movie makes gives u a starting part of movie in somewhere middle of movie(Gives you information something like” 15years before ” in movie)..by reading this words in movie you get to know the starting of movie… So likewise u place you main method anywhere in program(inside class) the JVM will read full code in its language to find main() method and starts acknowledging(executing) the code from main method… and if you dont provide main() method in code the JVM will not understand the story of your code will give error…

good explanation but give me parameters at the same time explanation. i mean which type of parameters in using java. and please give me different meanings Hello and hello in java

My program still runs if I use other word instead of args in “String[] args”(Parameter to main method). I want to know the reason behind it.

args is just a name of argument. You can use any name you want but type should be array of string, Hope it clarifies.

Very Good Explanation Thank you

great article.thnk uu

Thank you so much! that’s a good article. I appreciated you

i want more explanation. i know this information.

read java book. java complete reference

Good Article thanks

It’s really nice & understandable words.

Why “String[] ags” is passed as parameter of main function and without passing this parameter what will happen?

This is a standard which needs to follow if we want jvm to search it to execute it. There is no harm in removing this parameter from method but then that method will be treated as other java methods and it will not be directly called by JVM.

you will get a compilation error if you dont use that

The main method will compile successfully but will not run. As it will be treated as any other method. Certain standard to be followed for main method

good to know!!!

hi!!! Nice Explanation Thanx

String args[] : Parameter to main method…

Please site how to use the main() with values given to parameters (like what the difference would be with (or without) these values).

If u dont use it shows error in execution….that y we r passing infinite arguments in String type…string is one.type of class ….so,first letter is capital

Hello Sir , Nice Info wanted to ask what are the parameters does main method takes thanku

String args[]

Please fix your grammatical errors. It makes understanding this content difficult.

content is gud…dont alwayz look at grammar

u go for a tuition to learn grammar and become an English professor. you are learning java or checking english errors!

right ,we have developer ,most engineering pass out in first but those student not able to do coding this is different between software developer and english gentleman.

this is very nice info,thank you so much for author.

In this platform not a english debata,it is a programming language platform so does not matter english grammer.(some of the english grammer mistake so we will be adjust……)

You’re a bloody ass, Gaurang.

Hi, Good Explanation… Thanking you….

Leave A Comment Cancel reply

Your email address will not be published. Required fields are marked *

Save my name, email, and website in this browser for the next time I comment.

This site uses Akismet to reduce spam. Learn how your comment data is processed .

  • Demystifying “public static void main()”: Building a “Hello World” Java Application

By Alejandro Gervasio October 15, 2018 Java Core

The Java language is a neat example of the implementation of the OOP (object-oriented programming) paradigm. While it's fair to admit that mastering the paradigm's foundations is in most cases a difficult process, funny enough, Java itself is fairly easy to learn.

In this introductory article, we'll learn how to develop from scratch a simple Java application that prints out to the console the classic “Hello World” message.

Defining the “HelloWorldApplication” Class

To keep things simple, we'll create our example “Hello World” application without relying on any IDE in particular.

Therefore, let’s launch our preferred text editor, (e.g. NotePad on Windows or Text Edit on Mac), and write the following code:

Now, let's save the code snippet in a file called HelloWorldApplication.java , in the location of our choice in the file system.

While at first sight the example looks pretty naive, it actually covers a lot of background. Thus, let's break it down in several parts, so we can understand more clearly how it works:

  • The HelloWorldApplication class : we defined it with the public access modifier preceding the class keyword and the class name. This means that the class is public, hence we can access it from anywhere within the application, with no restrictions whatsoever.
  • The main() method is actually the sample application’s entry point. The public access modifier declares the method public, while that the static keyword implies that there’s no need to instantiate the class to call it. Lastly, the void keyword signals that the method returns no value(s).
  • The main() method arguments, String[] args: an array of strings , which we can use for passing command-line arguments to the method.
  • The System.out.println statement does exactly what we might expect: it prints out to the console the “Hello World!” text.

Compiling the “HelloWorldApplication” Class

At this point, HelloWorldApplication.java is just a plain text file with a fancy extension. To make it an executable file, though, first we need to compile it.

Compiling a Java file means basically converting the source code to bytecode, a format which can be interpreted and executed by the JVM (Java Virtual Machine).

Therefore, to compile our source file, let’s open up a command prompt. Next, let’s navigate to the location in the file system where we previously saved the HelloWorldApplication.java file, and type the javac command, followed by the name of the source file (the .java extension is required):

The javac command reads Java classes and interface definitions, and compiles them into bytecode class files. In addition, it can also process annotations in Java source files and classes.

Once we have run the javac command, a new executable HelloWorldApplication.class file should have been created in the same location of HelloWorldApplication.java .

Running the Executable File

The java command runs a Java program from a command prompt. When we invoke the java command, the JRE (Java Runtime Environment) is loaded, along with the supplied class. Finally, the class' main() method is executed.

In this case, the HelloWorldApplication class' main() method will be called. As a result, we should see the “Hello World!” text printed out in the console:

Further Improvements

Of course, there's plenty of room to improve the existing functionality of the HelloWorldApplication class.

We could refactor, for instance, the main() method, so it can take one or more command-line arguments and print them out to the console, along with the “Hello World!” message:

Likewise, we should first recompile the HelloWorldApplication.java file by using the javac command, and then rerun the class file by passing the command-line arguments to the java command:

As expected, the refactored example will generate the following output:

In this tutorial, we learned how to build from scratch a basic Java application. We deliberately kept all the examples easy to understand, as we put strong emphasis on learning a few key concepts of the Java language.

Here's a quick summary of them:

  • Class: the de-facto unit of data and behavior in Java, and the blueprint for objects. Objects or class instances can maintain internal state through one or more fields, and implement behavior via one or multiple methods.
  • The public access modifier: it allows us to declare the public visibility of class fields and methods.
  • Method or function: a code block container, which we can use to encapsulate behavior within a certain class.
  • Array: a dynamically-created container object that holds a fixed number of elements of the same type.

Featured Posts

  • An Introductory Look at Java Strings

Support this Site

As the old saying goes, content is king . Of course, delivering high-quality Java content in a reasonable timeline takes time and a lot of effort.

Do you like the current content and want to see many other Java topics covered in the near future? Then feel free to hire me , or please consider making a donation:

In either case, thank you for your support!

  • Java Core OOP
  • Design Patterns Spring
  • Hibernate Vavr
  • Bean Validation Java EE

What is Public Static Void Main in Java?

The public static void main() is the most important java method. The compiler starts executing the java program from the main method. Hope you remember when you wrote your's first java program.

Now, the question is what are these prefixes i.e public, static, and void in the main() method? What are the uses of these terms in these questions? Can we avoid prefixes? Let's dive deep, and find the in-depth knowledge of these terms.

Why Do We Use Public Static Void Main in Java?

The main() method is used to execute the java program, whether it is a small program or a large codebase program, Now the question is can we run the java program without implementing the main() method? The answer is no, every java program must have a main() method to execute the program.

Let's understand the execution life cycle of the java program.

During the execution of any java program, the JVM(java virtual machine) search the main() method of the java program to execute that's why the main() method is marked by public specifier because the JVM externally calls the main() method for executing, and the scope of public specifier allows to access it. Refer to this article to understand the scope of the different modifiers. Access Modifiers in java.

Now the question arises, what will happen if we try to use another specifier in the main() method in the place of the public. Let's see using an example.

We can see clearly the java compiler will throw an error. if we try to use another specifier instead of the public in the main() method .

Why main() Method in Java Must be Static?

Let's understand what is the use case of static and the importance of the static method in the main() method.

We know very well, that we need to create an object of the class to access its method, and creating an object requires extra space in the memory, and we know very well we can access the static method of the class without creating an object of the class. Refer to this article Static member.

Let's understand them without the help of some examples.

Examples of Public Static Void Main in Java

Avoiding the static keyword in the main method..

Let's understand what will happen if we avoid the static prefix in the main() method.

Changing the Position of the Public and Static keyword in the Main Method.

This is the most popular interview question that is what will happen if we interchange the position of the public and static keyword in the main() method.

Let's understand this using an example. In the example, we interchanged the position of the static and the public keyword.

We can interchange the position of the static and the public keywords in the main() method, but changing the position with other keywords like the void in the main() method will cause an error.

Parameter of the main() Method.

The main() method takes a String array[] as an argument that helps to take the value from the user.

Let's understand this using an example. In the below example, we are printing the length of the String array length, and the values of the first two indexes.

Learn more about Static Method in Java

Learn more about the static method in java Static method

  • JVM executes the main() method of the class.
  • The main() method must be of static and public type .
  • We cannot avoid the main() method if we want to execute the java program.

Code With C

The Way to Programming

  • C Tutorials
  • Java Tutorials
  • Python Tutorials
  • PHP Tutorials
  • Java Projects

Java’s Public Static Void – Why It Matters and What It Means

CWC

The Main method is a special method that is used to execute a program. When you run a java program in the command prompt then it calls main method. In this article, I am going to tell you why main method is public static void and why it is important to understand main method.

Java’s Public Static Void – Why It Matters and What It Means

Main method in java is the first entry point of a class. It is where you will start the execution of a class. This main method is public static void and you can’t change the main method. The main method in java is the entry point of the class and it’s always present in the class file. The main method in java is the first method that executes when you run a java program. If you are a beginner in java programming then you may not know the concept of main method in java. If you want to understand the main method in java, then you must read this article. Here are the 5 important reasons why main method in java is public static void.

What is the Main method?

The Main method is a method that is used to execute a program. The method contains the code that is used to run the program. The main method is a method which will be called when the program starts. The main method is required to be declared at the start of the code. This method will be executed when the application starts.

Main Method with Parameters: This is the most common type of the main method. Here you need to specify the arguments or the parameters.

Main Method without Parameters – If you don’t provide any arguments or parameters then it is known as the main method without parameters.

Main Method without Parameter and Argument: If you don’t provide any arguments or parameters then it is known as the main method without parameters and arguments.

Why Main method is public static void?

When you run a program in the command prompt, then it calls main method of the class. So, the main method is the first thing that will be called when you run the program. That means main method is the first thing to be executed. This is the reason that the main method is a static method.

A static method is a method that can be accessed without creating an object. When you create an object, it creates a new memory space, so it is impossible to access the static method without an object. So, main method is a static method and it can be accessed without an object.

Why public, private or protected?

The public method can be accessed from outside, the private method can be accessed only inside the class and the protected method can be accessible both within and outside the class.

Conclusion:

I hope this article will help you to understand the importance of the main method and how it is a static method. If you have any questions regarding this article, then you can leave a comment below.

You Might Also Like

Revolutionary feedback control project for social networking: enhancing information spread, the evolution of web development: past, present, and future, defining the future: an overview of software services, exploring the layers: the multifaceted world of software, deep dive: understanding the ‘case when’ clause in sql.

Go Programming language Create, Publish and Distribute Go Packages On pkg.go.dev

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Latest Posts

codewithc 61 Cutting-Edge Artificial Intelligence Project Unveiled in Machine Learning World

Cutting-Edge Artificial Intelligence Project Unveiled in Machine Learning World

75 Enhancing Exams with Image Processing: E-Assessment Project

Enhancing Exams with Image Processing: E-Assessment Project

73 Cutting-Edge Blockchain Projects for Cryptocurrency Enthusiasts - Project

Cutting-Edge Blockchain Projects for Cryptocurrency Enthusiasts – Project

67 Artificial Intelligence Marvel: Cutting-Edge Machine Learning Project

Artificial Intelligence Marvel: Cutting-Edge Machine Learning Project

84 Personalized Affective Feedback Project: Deep Learning Solutions for Student Frustration in IT

Personalized Affective Feedback Project: Deep Learning Solutions for Student Frustration in IT

Privacy overview.

en_US

Sign in to your account

Username or Email Address

Remember Me

  • Trending Now
  • Foundational Courses
  • Data Science
  • Practice Problem
  • Machine Learning
  • System Design
  • DevOps Tutorial
  • Java Methods
  • Parameter Passing Techniques in Java with Examples
  • Java is Strictly Pass by Value!
  • How are parameters passed in Java?
  • Method overloading and null error in Java
  • Can we Overload or Override static methods in java ?
  • Access specifier of methods in interfaces
  • Java main() Method - public static void main(String[] args)
  • Is main method compulsory in Java?
  • Understanding "static" in "public static void main" in Java
  • Valid variants of main() in Java
  • Replacing 'public' with 'private' in "main" in Java
  • Static Block and main() method in Java
  • GFact 48 | Overloading main() in Java
  • Static methods vs Instance methods in Java
  • enum in Java
  • Scope of Variables In Java
  • Can we override private methods in Java?
  • Private and final methods in Java

Replacing ‘public’ with ‘private’ in “main” in Java

Consider following Java program:

Explanation: 1) public : It is an access specifier which allows the JVM(Java Virtual Machine) to access the main method from anywhere. 2) static : static keyword allows the JVM to access the main method without any instance(object). 3) void : It specifies that the main method doesn’t return anything. 4) main : name of the method(function) configured in JVM. 5) String args[] : Command line arguments.

Now, if we replace ‘public’ with ‘private’ in “public static void main”, the above code becomes:

Explanation: The above code will be compiled successfully, but will throw a runtime error as follows:

Click to view output

Reason: Since the access specifier was changed from “public” to “private” JVM was unable to access/locate the main method.

Please Login to comment...

Similar reads.

  • Java Programs

Improve your Coding Skills with Practice

 alt=

What kind of Experience do you want to share?

IMAGES

  1. Understanding "static" in "public static void main" in Java

    write a case study on public static void main

  2. Explain about public static void main(String[] args)

    write a case study on public static void main

  3. public static void main(String[] args)

    write a case study on public static void main

  4. Méthode Java main()

    write a case study on public static void main

  5. What is public static void main in Java?

    write a case study on public static void main

  6. What is 'Public Static Void Main' in Java?

    write a case study on public static void main

VIDEO

  1. HOW TO WRITE CASE STUDY QUESTIONS?

  2. How to Write Case Study Begini Caranya #tirtamursitama #binusuniversity

  3. JAVA FAQ # 06 || Define public static void main method in Java

  4. Java Interview Question 118

  5. 9.About java main method

  6. public_static_void_by_rs_it_1.2_07

COMMENTS

  1. Java main() Method

    Apart from the above-mentioned signature of main, you could use public static void main (String args []) or public static void main (String… args) to call the main function in Java. The main method is called if its formal parameter matches that of an array of Strings. Note: Main Method is Cumpulosory in Java.

  2. Java Program Case Study on Public Static Void Main

    Case study: The program structure of a simple java program is given below with different steps. Step-1: Click start+run and then type notepad in run dialog box and click OK. It displays Notepad. Step-2: In run dialogbox type cmd and click OK. It displays command prompt. Step-3: Type the following program in the Notepad and save the program as ...

  3. What does `public static void main args` mean?

    main() must be declared as static because JVM does not know how to create an object of a class, so it needs a standard way to access the main method which is possible by declaring main() as static. If a method is declared as static then we can call that method outside the class without creating an object using the syntax ClassName.methodName(); .

  4. public static void main(String args[]){}

    void: It tells the return type of the method. "void" means the main method will not return any value. main: It is the name of the method. String args[]: It is the argument of the main method.

  5. Understanding public static void main(string args) in Core Java

    Public and static keyword position can be changed. There is no such rule of using public before static. But as void is method return type it should come just before method name which is main in this case. Hence this sequence. public static void main static public void main. Both are same. But as a standard we right as in the first case.

  6. public static void main (String [] args)

    public class Test {public static void main (String [] args) {System. out. println ("Hello, World!". In this article you'll learn what each component of the main method means.. Java Main Method Syntax. The syntax of the main method is always:. public static void main (String [] args) {// some code}. You can change only the name of the String array argument. For example, you can change args to ...

  7. Why public static void main() in Java instead of just void main()?

    The keyword static is used to achieve such purposes. In this case, use of static ensures that the main () method is intended to be invoked in a regular way rather than by objects. As far as main ...

  8. Demystifying "public static void main": Creating a "Hello World" Java

    The public access modifier declares the method public, while that the static keyword implies that there's no need to instantiate the class to call it. Lastly, the void keyword signals that the method returns no value(s). The main() method arguments, String[] args: an array of strings, which we can use for passing command-line arguments to the ...

  9. Public Static Void Main in Java

    The answer is no, every java program must have a main () method to execute the program. Let's understand the execution life cycle of the java program. During the execution of any java program, the JVM (java virtual machine) search the main () method of the java program to execute that's why the main () method is marked by public specifier ...

  10. Java's Public Static Void

    public class Main { public static void main() { // your code here } } Why Main method is public static void? When you run a program in the command prompt, then it calls main method of the class. So, the main method is the first thing that will be called when you run the program. That means main method is the first thing to be executed.

  11. What is 'Public Static Void Main' in Java?

    The keyword public static void main is the means by which you create a main method within the Java application. It's the core method of the program and calls all others. It's the core method of ...

  12. Java main() method

    The main () is the starting point for JVM to start execution of a Java program. Without the main () method, JVM will not execute the program. The syntax of the main () method is: public: It is an access specifier. We should use a public keyword before the main () method so that JVM can identify the execution point of the program.

  13. Main method in Java Programming Language

    In this tutorial, we'll delve into the Java main method, exploring its syntax, purpose, and significance in Java programs. We'll explain the meaning of each component of the main method signature: public, static, void, and the String [] args parameter. Join us as we unravel the intricacies of the main method, discussing its importance as the ...

  14. Quiz & Worksheet

    To pass the quiz, you'll need to: Define the void and public statement keywords in the public static void main statement. Know what the statement signifies in a public statement. Select the code ...

  15. Part (docx)

    Ace your courses with our free study and lecture notes, summaries, exam prep, and other resources

  16. Replacing 'public' with 'private' in "main" in Java

    Replacing 'public' with 'private' in "main" in Java. Consider following Java program: 1) public: It is an access specifier which allows the JVM (Java Virtual Machine) to access the main method from anywhere. 2) static: static keyword allows the JVM to access the main method without any instance (object). 3) void: It specifies that ...

  17. Writing a Unit test in Java with main method

    If you're attempting to assert values coming back, then your method signature is incorrect - it should be returning a String.From there, you can perform the usual operation with JUnit (your main method serves no purpose in this context): @Test public void checkString() { String expected = "My name is"; int actual = 3; // assuming words in string StringChecking testObj = new StringChecking ...

  18. c#

    static void Main() {…} static void Main(string[] args) {…} static int Main() {…} static int Main(string[] args) {…} As shown, the entry point can optionally return an int value. This return value is used in application termination (§10.2). Note: The above is quoted from the 4 th edition, now labeled "historical". The current edition is ...