Page 1 of 1

A closer look at the main method Rate Topic: ***** 1 Votes

#1 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

Posted 06 December 2007 - 10:01 AM

*
POPULAR

I've seen many beginner tutorials that give main method examples, but gloss over the details. That's understandable as it's probably confusing to a beginner. So, I'm here to fill in the blanks for those who want to know.

The Java main method looks like this:

public static void main(String args[])

We've all seen it I'm sure. As you can see it has three modifiers and one parameter, which is a string array. I will explain each, in detail where neccessary, below.

public - well, it has to be accessible outside the class, so it obviously has ot be public or Java cannot "see" it.

void - This means it returns no value. In C++ main can return an int value. This was usually used in unix systems to tell the thread that forked the process t hat is running the program, the exit status. Usually an exit status of 0 indicated normal termination. A non-zero exit status usually signified an abnormal termination with different integers corresponding to different "errors."

static - To understand why the main method must be static think about it this way. If a method is not marked static, then a class instance must be instantiated to call it. If it's static, you can call the method without instantiating an object.

public class SomeClass
{
  public int getInt() {return 0;}
  public static float getFloat() { return 1.0; }
}


Given this, I can call SomeClass.getFloat() directly, without creating a new instance of SomeClass. But, to call getInt() I must:

SomeClass c = new SomeClass();
c.getInt();


So, when Java looks for the entry point in a program (the main method), it must be able to call it without instantiating an object, hence the method must be static.

String args[] - This is used for command-line arguments. Say I have the following class:

public class Person
{
  private String firstName;
  private String lastName;

  public Person(String firstName, String lastName)
  {
	 this.firstName= firstName;
	 this.lastName= lastName;
  }

  public String toString()
  {
	  System.out.printf("Hello.  My name is %s %s/n", firstName, lastName);
  }

  public static void main(String args[])
  {
	 Person p = new Person(args[0], args[1]);
		 p.toString();
  }
}


If I now execute this class from the command-line, as follows, java Person John Smith, then the strings "John" and "Smith" will be passed to main (in the args[] array) in the left-to-right order in which they were written. In other words args[0] == "John" and args[1] == "Smith". So the output of that should be, Hello. My name is John Smith.

As an extra aside, the main method of a class cal be called from another class. That's right. Java uses it as the entry point of a program, but it is still just a static method. So, from within another class I can do the following:

...
String myArgs = new String[] {"John", "Smith"};
Person.main(myArgs);
...


This is equivalent to the previous command-line execution, java Person John Smith, and should output the same thing: Hello. My name is John Smith. I've used this method of calling another program before, but at the moment I can't recall why. It can be useful for testing purposes, I suppose, although many IDE's provide a way to pass in command-line args.

Hopefully someone finds this useful. If you see any errors, please let me know. Thanks,

Is This A Good Question/Topic? 5
  • +

Replies To: A closer look at the main method

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4422
  • View blog
  • Posts: 12,289
  • Joined: 18-April 07

Posted 06 December 2007 - 10:13 AM

Good job! Simple and straight to the point. I am sure many beginners have always wondered about that. Your explanations also offer a glimmer of light into other programming ideas including object instantiation, calling functions with parameters, and the idea behind static method calls.

:)
Was This Post Helpful? 0
  • +
  • -

#3 bhandari  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 9
  • View blog
  • Posts: 754
  • Joined: 31-January 08

Posted 01 February 2008 - 08:56 AM

Some important facts
1) main method can be overloaded by providing a different argument list, but that overloaded method won't be called by JVM
2) you can't return any thing from main method , if you do, compiler has no problems but JVM has. The return type has to be void for the main version called by JVM
3) Only static member variables (primitives or object references) can be accessed by the main method as it itself is a static method. (Though you can always create an instance of the this class and then use the non-static members)

In a nutshell, JVM always looks for public static void main(String arg[]) as the starting method.
Was This Post Helpful? 1
  • +
  • -

#4 myra  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 04-April 08

Posted 27 April 2008 - 05:12 AM

Great tutorial :^: Keep it up :D
Was This Post Helpful? 0
  • +
  • -

#5 herefishyfishy  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 60
  • Joined: 01-May 08

Posted 06 May 2008 - 02:16 PM

Quote

String args[] - This is used for command-line arguments. Say I have the following class:

public class Person
{
  private String firstName;
  private String lastName;

  public Person(String firstName, String lastName)
  {
	 this.firstName= firstName;
	 this.lastName= lastName;
  }

  public String toString()
  {
	  System.out.printf("Hello.  My name is %s %s/n", firstName, lastName);
  }

  public static void main(String args[])
  {
	 Person p = new Person(args[0], args[1]);
		 p.toString();
  }
}



If I now execute this class from the command-line, as follows, java Person John Smith, then the strings "John" and "Smith" will be passed to main (in the args[] array) in the left-to-right order in which they were written. In other words args[0] == "John" and args[1] == "Smith". So the output of that should be, Hello. My name is John Smith.


Why does the line say
p.toString();

rather than
System.out.print(p);
?
p.toString(); doesn't really do anything.

Anyways, great tutorial otherwise.
Was This Post Helpful? 0
  • +
  • -

#6 Ellie  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 8
  • View blog
  • Posts: 533
  • Joined: 17-January 07

Posted 08 May 2008 - 03:44 AM

View Postherefishyfishy, on 6 May, 2008 - 10:16 PM, said:

Why does the line say
p.toString();

rather than
System.out.print(p);
?
p.toString(); doesn't really do anything.

Anyways, great tutorial otherwise.


It says that because Programmist has overridden the toString method such that is does print the string.

Quote

public String toString()
{
System.out.printf("Hello. My name is %s %s/n", firstName, lastName);
}


:^:
Was This Post Helpful? 0
  • +
  • -

#7 dreamerGon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 26-May 08

Posted 26 May 2008 - 03:18 AM

Quote

public String toString()
{
System.out.printf("Hello. My name is %s %s/n", firstName, lastName);
}


I don't think that toString method will work since it has a return type "String" and is not returning anything. However, that's pretty irrelevant to the main point.

This post has been edited by dreamerGon: 26 May 2008 - 03:19 AM

Was This Post Helpful? 0
  • +
  • -

#8 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Posted 26 May 2008 - 04:15 AM

That will probably result in a compilation error (missing return statement).
Was This Post Helpful? 0
  • +
  • -

#9 herefishyfishy  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 60
  • Joined: 01-May 08

Posted 29 May 2008 - 05:22 PM

Oh, I didn't notice that.
Was This Post Helpful? 0
  • +
  • -

#10 Guest_abcd*


Reputation:

Posted 14 February 2011 - 11:50 PM

sorry friend u r indirectly saying that an instance method should be called by an object and a ststic ones with classname its not like that friend it is that when a method or a variable should be declared as a static is when a given method is called only once through out the program then it should be declared as a static and coming to variable it should be static when the value of it is a fixed ones that is some pi=3.14 like that
Was This Post Helpful? -1

Page 1 of 1