Forums Register Login

main method parameter

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Hi,
I wonder that why we can only use main method in public class.
If anyone know the reason, please inform me.

Thanks,

Mert
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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

 
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Thanks for all replies...
His brain is the size of a cherry pit! About the size of this ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1108 times.
Similar Threads
Difference between Arguments and parameters
Is execute() method always executed? (Struts 1)
Polymorphism
Garbage Collection
valid syntax
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 09:19:21.