wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes main method parameter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "main method parameter" Watch "main method parameter" New topic
Author

main method parameter

mert �zkaya
Ranch Hand

Joined: Jan 26, 2006
Posts: 33
Hi everyone,
We always use main method in order to execute some metods ,statement or instance of any class. But in fact , I still do not know why we always write String args[] parameter.
If anyone know the reason , please inform me.

Regards,

Mert
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
If you execute a Java program from the command line (or by calling the main method in question directly), you can pass arguments into the main method to be processed by the method. For example:

Using this code you could concatenate two files into one larger file. From the command line you would call:

These three arguments would be passed to the main method as a String[] locally called args.


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
mert �zkaya
Ranch Hand

Joined: Jan 26, 2006
Posts: 33
Hi,
I wonder that why we can only use main method in public class.
If anyone know the reason, please inform me.

Thanks,

Mert
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41124
    
  45
It's simply defined that way. If you start the "java" executable with a class name, it will look for a method declared as "public static void main (String[])" in that class to execute. It doesn't matter whether you actually have any parameters - you can't leave out the String array. This is very similar to how executables written in C work.


Ping & DNS - my free Android networking tools app
mert �zkaya
Ranch Hand

Joined: Jan 26, 2006
Posts: 33
Hi,
I also did not understand the logic of this sentences in spite of I know that it is true.
Can anyone explain it ??

Static methods can't use any non-static variables or methods in the class with the implied this, since there is no this object
Sathiq Ali
Greenhorn

Joined: Jan 26, 2006
Posts: 9
Hi,
That statement is telling "U can not acces any nonstatic members by using static methods". i.e we can not call the non static method or members inside a static method.
Sathiq Ali
Greenhorn

Joined: Jan 26, 2006
Posts: 9
Hi,
That statement is telling "U can not acces any nonstatic members by using static methods". i.e we can not call the non static method or members inside a static method without creating object. But by creating an object , we can use it.
mert �zkaya
Ranch Hand

Joined: Jan 26, 2006
Posts: 33
Thanks for all replies...
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: main method parameter
 
Similar Threads
Is execute() method always executed? (Struts 1)
Difference between Arguments and parameters
valid syntax
Polymorphism
Garbage Collection