What is public static void main

what is public static void main

public static void main(String[] args) Java main method

Aug 25, Every word in the public static void main statement has got a meaning to the JVM. Public: It is an Access modifier, which specifies from where and who can access the method. Making the main() method public makes it globally available. It is made public so that JVM can invoke it from outside the class as it is not present in the current class. Nov 25, public static void main (string args []) Explanation November 25, JBT In Java, JVM (Java Virtual Machine) will always look for a specific method signature to start running an application, and that would be public static void main (String args []). Here args is an argument of the type String array.

Join Stack Overflow to learn, share puvlic, and build your how to open english leather cologne. Connect and share knowledge within a single location that is structured and easy to search. I'm in the process of learning. In all the examples in the book I'm working from public static statix comes before any method that is being used or created.

What does this mean? Other alternatives are privateprotectedpackage and package-private. See here for more details. This means that you can call a static method without creating an object of the class. If the method returned an int you would write int instead of void.

The combination of all three of these is most commonly seen on the main method which most tutorials will include. It is more or less a function. The public keyword is an access specifier, wwhat allows the programmer to control the visibility of class members. When a class member is preceded by public, then that statuc may be accessed by code outside the class in which it is declared. The opposite of public statid private, which prevents a member from being used by code defined outside of its class.

In this case, main must be declared as public, since it must be called by code outside of its class when the program is started. The keyword static allows main to be called without having to instantiate a particular instance of the class. This is necessary since main is called by the Java interpreter before any objects are made.

The keyword void simply tells the compiler that main does not return a value. As you will see, methods may also return values. You'd think that the lack of a return means it isn't doing much, but it might be saving things in the database, for example. Since you are just learning, don't worry about the first two too much until you learn about classes, and the third won't matter much until you start writing functions wjat than main that is.

Best piece of advice I got when learning to program, maih which Whwt pass along to you, is don't worry about the little details you don't understand right away. Get a broad overview of the fundamentals, then go back and worry about the details. The reason sgatic that you have to use some things like public static void in your first programs which can't really be explained well without teaching you about i bunch of other stuff makn.

So, mxin the moment, just accept that that's the way it's done, and move on. You will understand them shortly. Considering the typical top-level class. Only public and no modifier access modifiers may be used at the top level so you'll either see public or you won't see any access pub,ic at all.

There are other reasons why you wouldn't include static too but this is the typical si at the top level. Disclaimer: I'm a newbie myself so if this answer is wrong in any way please don't hang me.

By day I'm a tech recruiter not a developer; coding is my hobby. Also, I'm always open to constructive criticism and love to learn so please feel free to point out any errors.

Static - creates a class. Void - this means that no product is returned when the class completes processing. Compare this with helper classes that provide a return value to the main class,these operate like functions; these do not have void in the declaration. Because of use of a static keyword main is your first method to be invoked. Satic are we doing? Please help us improve Stack Overflow. Take our short survey. Stack Overflow for Teams Collaborate and share knowledge with a private group.

Create a free Team What is Stxtic Learn more. What does 'public static void' mean in Java? Ask Question. Asked 11 years, 1 month ago. Active 2 years, 4 months ago. Viewed k times. What does public static dr susses how the grinch stole christmas mean in Java? Bill the Lizard k gold badges silver badges bronze badges.

David David Add a comment. Active Oldest Votes. It's three completely different things: public means that the method is visible and can be called from other objects of other types. Lloyd Nicholson 3 3 silver kain 12 12 bronze badges. Mark Byers Mark Byers k gold badges silver badges bronze badges. Since the questioner is still what is public static void main the order of these keywords is also important.

All modifiers first publicstaticprivateetc. The three words have orthogonal meanings. It means "this method returns nothing". Thomas Pornin Thomas Pornin It means that: public - it can be called from anywhere static - it doesn't have any object state, so you can call it without instantiating an object void - it doesn't return anything You'd think that the lack of voix return means it isn't doing much, but it might be saving things in the database, for example.

Paul Tomblin Paul Tomblin k whwt 56 gold badges silver badges bronze badges. It means three things. First pjblic means that any other object can access it. StudioTime Aaron Aaron 1, 6 6 silver badges 16 16 bronze badges. Gilles 'SO- stop being evil' Saleem Bhayo Saleem Bhayo 41 1 1 bronze badge. This question has been adequately covered, perhaps try answering new questions instead of answering older questions with duplicate information.

It's just like explaining about elephant. People try to explain it depend on where he stands. I am happy with all the answers above, but just wonder how can any one say "Buy a book an java". Public what is public static void main means that the class program is available for use by any other class.

Saghir A. Khatri 3, 6 6 gold badges 40 40 silver badges 73 73 bronze badges. The Overflow Blog. Podcast How to build and maintain online coid, from gaming to. Level Up: Creative Coding with p5. Featured on Meta. Stack Overflow for Teams is now free for up to 50 users, forever. Outdated Answers: results from use-case survey.

Downvotes Survey results. Linked 0. Related Hot Network Questions. Question feed. Stack Overflow works best with JavaScript enabled. Accept all cookies Customize settings.

1. Java main method syntax

Nov 02, It is the access modifier that means main () method can be accessed from everywhere. The public keyword, allows the programmer to control the visibility of class members. When a class member is preceded by public, then that member can be accessed by code outside the class in which it is declared. public static void main(String[] args) Java main method is the entry point of any java program. Its syntax is always public static void main(String[] args). You can only change the name of String array argument, for example you can change argsto myStringArgs. Sep 10, Hence making it a convention to make the entry method main() as public static void main(String[] args) is convenient. What if we dont write static before the main method: If we do not write static before the main method then, our program will be compiled without any compilation error(s). But at the time of execution, the JVM searches for the main method which is public, static, with a .

When you start learning java programming, this is the first method you encounter. Java main method is the entry point of any java program. Its syntax is always public static void main String[] args. You can only change the name of String array argument, for example you can change args to myStringArgs. Also String array argument can be written as String This is the access modifier of the main method.

It has to be public so that java runtime can execute this method. So it means that the main method has to be public. When java runtime starts, there is no object of the class present. Java programming mandates that every method provide the return type. This has been done to keep things simple because once the main method is finished executing, java program terminates. So there is no point in returning anything, there is nothing that can be done for the returned object by JVM.

If we try to return something from the main method, it will give compilation error as an unexpected return value. For example, if we have the main method like below. This is the name of java main method.

For example, if we have a class like below. Java main method accepts a single argument of type String array. This is also called as java command line arguments. Above is a simple program where we are printing the command line arguments.

Below images show how to pass command line arguments when you are executing a java program in Eclipse. Correct the above code.

You need to mention the condition in the mid statement of the loop, as compiler is unable to run the loop without knowing the condition true or false.

My question is that 1 what are the maximum possibilities to write in different string names? It will be exactly the same. OK sir, thak you. By the way, I was preparing for my java programming exams, and have done paper in month of march. But I glad that you removed my confusion. I was written 2 main method in single main class,But i am not getting 2 output like Show screen,sucess shows. When you execute a java class, method with the signature public static void main String[] args gets executed.

Why do you sometime type: public static void mymain String args[ ] and other times: public static void mymain String [ ] args? What is the correct sintax? Both syntaxes are correct. But the recommended and conventional one is String [ ] args. When you run a java program then JVM will search for main method in the class with String []args if it not present then it will not print anything.

When JVM starts, it does not have any instance of the class having main method. So static. Sir i am new to java. I have compiled and run about 30 java programe but now in a specific folder java programe comiled but wont run showing error main method not found even i have main method psvm S[] args.

Return type is not part of the method signature. Nice explanation though! Thanks for noticing the typo. Can you explain as to why do we need to have a String args[] array passed to main? Why does it not compile if we do not pass a String args[] array? Why does Java need a String args[] array? Try to avoid saying things like your idea fails. Made minor changes. Changed the first letter of the class to uppercase, but you might not have to. Sir I want to implement push notifications like your site.

My website is built on spring and jsp. Please help me out of this. Please look into their documentation for implementing it. You can also look at some other services for push notification. Your email address will not be published. I would love to connect with you personally. Table of Contents 1 public static void main String[] args 1.

Prev Java Anonymous Class. Next json-simple example. Pankaj I love Open Source technologies and writing about my experience about them is my passion. Follow Author. Comments Ackeem says:. April 7, at pm. Dia says:. October 28, at pm. December 28, at am. August 26, at pm. April 28, at am. Haribabu says:. July 5, at am. Ramya says:. December 10, at am. Ali says:. February 2, at pm.

Fareha says:. March 11, at am. August 11, at am. August 21, at pm. Ricardo says:. February 6, at pm. December 11, at pm. Pankaj says:. Ram says:. April 22, at am. Ben says:. November 15, at am. October 29, at am.

October 23, at am. October 23, at pm. Miguel Ferreira says:. October 21, at am. October 21, at pm. September 30, at am. October 8, at pm. DileeP Achar says:. August 17, at am. June 14, at am. April 10, at pm. Jagadeesh Keerthi says:. June 2, at pm.

Sawan sharma says:.

5 Comments:

  1. Jaya K and changed in pretty much every aspect to fit in modern social standards that steal its original charm.

  2. I prefer the white balance, shadows, and general colour palette of the original

Add a comment

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