Results 1 to 19 of 19
Thread: main method
- 06-24-2011, 04:00 AM #1
Member
- Join Date
- Jun 2011
- Location
- New Zealand
- Posts
- 8
- Rep Power
- 0
main method
Hi,
Can you explain to me how the main method works.
Why each keyword is needed for the main method to function.
Is the args parameter needed with this basic output command?
Sorry if this is too basic for you, Im very new to Java.Java Code:public static void main(String args[]) { System.out.println("Hello World"); }
As simple as possible please. Thanks.
- 06-24-2011, 04:29 AM #2
When you run your program you actually start the JVM which then calls YourProgram.main(args). This is why the method needs to be static and accept a String array as a parameter. Otherwise the JVM cannot find it.
- 06-24-2011, 04:40 AM #3
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
I am not an expert so my explanation is base on what I understand and might not as deep as how others understand it. main method is the first thing that JVM search for and where your program begin. Every application (Applet is excluded) should have main method, as I said, this is the first thing that JVM searches.
String[] args is not actually needed, do not get me wrong, what I mean is it is optional. It depends on the program if you will require it or not.
Here are two example, one requires your input before program executes. The other one executes then ask for your input
in your command line you will type something like
java className mine0926
in your command line you will type something likeJava Code:public static void main(String[] args) { String yourName = args[0]; //<-- args[0] is your input which is mine0926 System.out.println("Your name is : " + yourName); }
java className
Java Code:public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("What is your name?"); String yourName = scanner.readLine(); System.out.println("Your name is : " + yourName); }
- 06-24-2011, 04:44 AM #4
- 06-24-2011, 05:00 AM #5
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
That is what I am trying to explain, thanks for clarifying it, :)
- 06-24-2011, 06:27 AM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
In case you don't know all the modifiers:
Public is access control, it allows the method to be accessed from anywhere(you can also have private, protected, and default(package access) play around with different access modifiers for main and see how the jvm reacts)
Static is the type of method, a static method can be accessed without first creating an instance of the class. You can reference static methods with ClassName.MethodName, the methods on the math class are mostly static(I say mostly because I'm not certain that none are static)
Void is the return type, a method that has a void return type simply does something and does not return anything.
- 06-24-2011, 08:18 AM #7
Member
- Join Date
- Jun 2011
- Location
- New Zealand
- Posts
- 8
- Rep Power
- 0
main() is where a program always start. I tried making it private, protected, and package but those three didnt work
so therefore main() needs to be public because the JVM calls the main method outside of its class.
It need to be static so that the main method can be called before making any objects of the class.
It is void because it didnt return anything but is it possible to change void into something else that would return something?
Thanks for the examples. I tried running this but it doesn't seem to work. But I can see how the code work: when the user types in there name, itJava Code:public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("What is your name?"); String yourName = scanner.readLine(); System.out.println("Your name is : " + yourName); }
returns the typed name.
So it is up to me if I want to use the args parameter or not. But are there any cases when it is necessary to use the parameter? It does seem a bit inconvenient.
- 06-24-2011, 08:24 AM #8
Try it and see.
Explain what "doesn't work" means. There could be a gazillion different causes.I tried running this but it doesn't seem to work.
No. Since the programmer has to write the main method what happens in that method is totally up to the programmer. You get to choose. The only thing that you are forced to do is write a main method with that exact signature.So it is up to me if I want to use the args parameter or not. But are there any cases when it is necessary to use the parameter? It does seem a bit inconvenient.
- 06-24-2011, 08:38 AM #9
Note that Applets don't require a p.s.v.main(...) method, nor do MIDlets (Java ME). The p.s.v.main(...) method is the entry point for applications.
db
- 06-24-2011, 09:08 AM #10
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
It will not work because it is not in a class. We said before that an application must have main method, in additional main method should be place in a class.
So jvm will call YourClassName.main() as a beginning of your application.
The two example that I show do same thing, the only difference I how the programmer ask the user to input user's name.
I just want to clear up that main method did not return the input but rather print what has been inputted. The word "return" means something in programming language especially in java.
- 06-24-2011, 09:26 AM #11
Member
- Join Date
- Jun 2011
- Location
- New Zealand
- Posts
- 8
- Rep Power
- 0
I tried putting an int instead of void and it gave me an error even though I returned an int.
Sorry I was talking about the code above. I tried running it and it gave an error "The method readLine() is undefined for the type Scanner"Java Code:public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("What is your name?"); String yourName = scanner.readLine(); System.out.println("Your name is : " + yourName); }
Maybe readLine() is not a method in Scanner class.
I can see how important the main method is. Thanks for giving me a clear idea about how the main method works. If there are anything I need to know more about the main () method, it would be nice if you could tell me. But I guess this info will suffice for now.
- 06-24-2011, 09:33 AM #12
Time for a link: Trail: Learning the Java Language (The Java™ Tutorials)
You can skip ahead and go through Lesson: Classes and Objects (The Java™ Tutorials > Learning the Java Language) then start over.
db
- 06-24-2011, 09:36 AM #13
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
Sorry for not checking and testing it, change readLine() to nextLine()
- 06-24-2011, 09:43 AM #14
Member
- Join Date
- Jun 2011
- Location
- New Zealand
- Posts
- 8
- Rep Power
- 0
In other words it prints the input after the user types a name, not return. return is when you return a value like an int, double, boolean, etc. I'll definitely put it in mind. Thanks.I just want to clear up that main method did not return the input but rather print what has been inputted. The word "return" means something in programming language especially in java.
- 06-24-2011, 09:53 AM #15
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
Yes. main method should always be written the same way the only thing you can change there, if I am not mistaken, is the name of string which is "args". But the standard way of writing it, mosty programmers write it so anyone who read it will automatically know that it is the main method, is public static void main(String[] args).
- 06-24-2011, 10:08 AM #16
The parameter name, which is of type String[], not String. But yes, it's entirely legal to change that name e.g.the only thing you can change there, if I am not mistaken, is the name of string which is "args"dbJava Code:public static void main(String[] arrrrrgghhhh) { // }
- 06-25-2011, 01:38 PM #17
Member
- Join Date
- Jun 2011
- Posts
- 3
- Rep Power
- 0
pub;ic implies accesed any where inside package
static implies it can be called only once
void implies does not return any thing
string[] args : this implies that u can pass array of strings to main method as input
please see my blog Moderator edit: link removedLast edited by DarrylBurke; 06-25-2011 at 05:13 PM. Reason: Removed link
- 06-25-2011, 01:46 PM #18
Not true. A static method can be called repeatedly as many times as you want.static implies it can be called only once
"implies" is redundant herevoid implies does not return any thing
- 06-25-2011, 01:47 PM #19
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Static doesn't imply that a method can only be called once. Not only is it possible, but very possible to create a recursive static method. You can also call main inside main(although this will cause infinite recursion, it is possible)
As far as I know this works, you can test it and verify.Java Code:public class X{ public static void main(String[] args){ X.main(args); } }
Similar Threads
-
Running main method class from another main class
By tlrocketman in forum New To JavaReplies: 3Last Post: 12-06-2010, 08:30 AM -
Calling The main method from another method
By SwissR in forum New To JavaReplies: 3Last Post: 07-27-2010, 11:03 AM -
Main Method Help
By thomas6 in forum New To JavaReplies: 0Last Post: 03-12-2010, 01:20 AM -
calling method from main method
By bob_bee in forum New To JavaReplies: 4Last Post: 10-02-2009, 05:30 PM -
Where do I put main method?
By jerryrice80 in forum New To JavaReplies: 4Last Post: 09-16-2009, 08:23 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks