Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

main method parameter

 
mert �zkaya
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
mert �zkaya
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
mert �zkaya
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all replies...
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic