Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

public static void main method

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In java everything is based on a class
So , where or in which class is the "public static void main(String args[])" method defined?
 
tumbleweed
Posts: 5089
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"public static void main(String args[])" method is used in standalone programs. It its the start of a program.
 
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can put a "main" in any of your classes. You tell the system which one to run whan you type "java MyClass", so there's nothing stopping you haveing an application which can be run in different ways for different purposes by specifying different classes to run.
 
Sanatan Satyapriy
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand that we use the public static void main method to start the execution of a java standalone application.
So, if the main method is present in class XYZ then we say
java XYZ at the command prompt
What i would like to know is
Is this method a part of some class in the java API. Is it declared in some class of java API and we override that method in our class
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

So, if the main method is present in class XYZ then we say
java XYZ at the command prompt
What i would like to know is
Is this method a part of some class in the java API. Is it declared in some class of java API and we override that method in our class[/B]
>> "main" is the entry point of the application that java interpreter looks at, for running your application. We don't override that method. It is a special method with a unique signature that we need to provide in your main class (XYZ).
---
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Main is a static method in java. So you can run this method without creating a instance of the class or you can create an instance within the main method.It only helps to test the proper functioning of the class.
 
Sanatan Satyapriy
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is absolutely clear that public static oid main is a static method which can be called to start the execution without creating an instance of the class; Now here is the confusion :
Even though the public static void main method is static ; as in case of other methods it should have been defined in a particular class, because methods and variables are declared and defined within the body of a class.
So what I want to know is :
Is there a class in the java API where the public static void main method is defined?
 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My guess would be that it is defined in Object, since that is the root of all classes.
------------------
  • Ryan Burgdorfer
  • Java Acolyte in
  • Columbus, OH USA
 
Sanatan Satyapriy
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is absolutely clear that public static void main method is a static method which can be called to start the execution without creating an instance of a class.
Here is the confusion:
Which class are we talking about?
Because as with other methods , whether they are static or non-static they are defined in a class.
So the point of my query is : Is public static void main method a part of any class in the Java API?
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure what you are talking about. The main method is located in whatever class it is in. So if I want to run class XYZ from the prompt then I will add a main method to class XYZ.
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Is this method a part of some class in the java API. Is it declared in some class of java API and we override that method in our class

The answer is no. When you run something from the prompt (java XYZ) then the java executable will run the main method of whatever class you specify. If there is no main method then you will get an error.
 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,Sanatan
I suppose that the main method is not a object issue, have nothing to do with the concept of inheritence or like.i think it's just like a hundle of any class for jvm to hold to execute the class.you know, the jvm only recognize "main" with the specific signature.when you run it from the prompt, the jvm find the "main" method rather than "***" method, why? and can we write some other"***" method in place of the predefined "main"? no, we can't, because java doesn't provide any way to do so! it just only recognize the word "main", of course, with the determined signature. It works as if the word "main" is one of java's key words.
In sum, my guess is that the main method is a built-in approach for jvm to run a class. there's no logic rather than a hardcoded rule here.
All above is my personal guesswork, hope some guru could enlighten me if i'm in the wrong track.
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sanatan-
Here's what I found from the documentation & the Java Tutorial.


Every Java application must contain a main method whose signature looks like this:
public static void main(String[] args)
When the Java interpreter executes an application (by being invoked upon the application's controlling class), it starts by calling the class's main method. The main method then calls all the other methods required to run your application.
If you try to invoke the Java interpreter on a class that does not have a main method, the interpreter refuses to run your program and displays an error message similar to this:
In class NoMain: void main(String argv[]) is not defined
From The Java Tutorial


The Java virtual machine starts up by creating an initial class, which is specified in an implementation-dependent manner, using the bootstrap class loader (�5.3.1). The Java virtual machine then links the initial class, initializes it, and invokes its public class method void main(String[]). The invocation of this method drives all further execution.
From JVM Specification 2nd ed. Section 5.2


No, the main() is not in the API/in some class rather it is a requirement of the JVM
James, I just found some text to support your answer.
regards,
Jyotsna
[This message has been edited by Jyotsna Clarkin (edited March 29, 2001).]
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was surprised to see the following signature work on my java class
private static void main(String[] agrv)
Any ideas ?
Thanks.
-vasudeva
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can't make main private, so guess what? The JVM ignores it and treats it as public.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic