• Post Reply Bookmark Topic Watch Topic
  • New Topic

why should i use public static void main(String []p)  RSS feed

 
venu jayaram
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please tell me
public : is for access modifer
static : ?
void :no return type in main
main: method name
String []p: command line argument,

please tell me why we need static in main method.
[ April 22, 2008: Message edited by: venu jayaram ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

A static method is one that's associated with a class, but can be called without creating an instance of that class. Most methods (i.e., non-static ones) require an object to be called, but static ones do not.

The real answer is, of course, that this is just how they chose to do it. A non-static main() would have worked just as well; the JVM could create an instance of main()'s class to call it if it needed to. But since the JVM is designed to expect a static method, that's what you must use.
 
venu jayaram
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
Ernest Friedman-Hill


one more thing if in main

public static void mian(String p[])

insted of this

public static void mian(String )

in inertview they asked me this ?

1st i told it will give compilation error,i sayed we are passing object string array
that is of refrence varible.
 
Jaikiran Pai
Sheriff
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to our JiG(B) forum.
 
Piet Verdriet
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by venu jayaram:
hi
Ernest Friedman-Hill


one more thing if in main

public static void mian(String p[])

insted of this

public static void mian(String )

in inertview they asked me this ?

1st i told it will give compilation error,


public static void mian(String ) will give a compilation error since you did not add a variable name after String.
public static void mian(String p[]) will compile just fine. Note that you cannot run a class with that method since the JVM expects a main(...), not a mian(...) method (the 'i' and 'a' are switched).


Originally posted by venu jayaram:
i sayed we are passing object string array
that is of refrence varible.


I don't know what you mean by that.
[ April 22, 2008: Message edited by: Piet Verdriet ]
 
Sidharth Pallai
Ranch Hand
Posts: 137
Hibernate Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jaikiran Pai:
Moving to our JiG(B) forum.


It wont give a compilation error but rather a run-time exception- NoSuchMethodError, because JVM need the required signature which is as follows "public static void main(String[] jaikiran){...}" Though the method can be overloaded but JVM will only invoke the one with above signature.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!