IITDU Forum
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Java Main Method

4 posters

Go down

Java Main Method Empty Java Main Method

Post by BIT0102-Mohaimin Mon Sep 13, 2010 2:36 pm

Several times junior mates at IIT, who just started Java, asked me to explain the signature of the Java main method.

The signature:

Code:
public static void main(String[] cArgs);

I am trying to explain each word separately.

public:

public is one of the four (unlike C++, where it is three) access modifier. By declaring something public, you are telling that the code-block can be accessed from outside the class. Main is the point to start execution. So, it must be accessed by the compiler directly from outside. If you declare main something other than public the source will successfully compiled. But if you try to run, the compiler will reply:

Code:
Main method not public

static:

The big question I always had and I am asked several times. static methods or variables are those, which are instantiated before any objects are instantiated. On the other hand we can say, If anything is required to be instantiated before any objects have been instantiated, you need to declare it static.

Now, as main is the entering point of a program, it must be called before any thing else has happend. So, you have to make it static. Otherwise compiler will say,

Code:
Exception in thread "main" Java.lang.NoSuchMethodError: main

void:

This is the return type. Java main() does not return anything. So, the return type is void.

cArgs:

This is the command line argument. The type is array of String. Beginners, please note that cArgs is just a variable name. You can use anything you like. If you know C/C++, you may know that command line argument is optional there, but that is not true in case of Java.

If you need to learn about command line argument. You can consult here.
BIT0102-Mohaimin
BIT0102-Mohaimin
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 415
Points : 715

Back to top Go down

Java Main Method Empty Re: Java Main Method

Post by BIT0122-Amit Mon Sep 13, 2010 11:20 pm

Nice one Smile
However, I think cArgs is somewhat confusing to beginners.
Though it is a variable name, most of the time people use args[] .


Anyway, very informative post Mohaimin Smile
BIT0122-Amit
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

https://iitdu.forumotion.com

Back to top Go down

Java Main Method Empty Re: Java Main Method

Post by BIT0102-Mohaimin Sat Sep 18, 2010 1:02 am

BIT0122-Amit wrote:
However, I think cArgs is somewhat confusing to beginners.
Though it is a variable name, most of the time people use args[] .

It was intentional, to attract beginners' attention. Many beginners' think it is fixed to args.
BIT0102-Mohaimin
BIT0102-Mohaimin
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 415
Points : 715

Back to top Go down

Java Main Method Empty Re: Java Main Method

Post by BIT0122-Amit Sat Sep 18, 2010 1:09 am

Hmm.... then maybe you should explain in the later part of title post that... they should use args Neutral
BIT0122-Amit
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

https://iitdu.forumotion.com

Back to top Go down

Java Main Method Empty Re: Java Main Method

Post by BIT0112-Rokon Thu Aug 18, 2011 2:07 pm

I have a question, Can the main( ) method be specified as private or protected? if, how?
I think, it should be, I have an idea how to do this. What do you think?
BIT0112-Rokon
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

http://blog.codexplo.org

Back to top Go down

Java Main Method Empty Re: Java Main Method

Post by BIT0322-Hira Sat Aug 20, 2011 2:55 am

"static methods or variables are those, which are instantiated before any objects are instantiated. On the other hand we can say, If anything is required to be instantiated before any objects have been instantiated, you need to declare it static."
What does it mean?

BIT0322-Hira
Alpha Release
Alpha Release

Course(s) :
  • BIT

Blood Group : O+
Posts : 24
Points : 33

Back to top Go down

Java Main Method Empty Re: Java Main Method

Post by BIT0112-Rokon Sat Aug 20, 2011 6:07 am

BIT0322-Hira wrote:"static methods or variables are those, which are instantiated before any objects are instantiated. On the other hand we can say, If anything is required to be instantiated before any objects have been instantiated, you need to declare it static."
What does it mean?

Let me tell you a scenario, you have written a piece of java code, that is a java class. The java class is loaded to compile in java compiler. Java compiler compiles it to byte code, and then run it. For simplicity let, the whole things are done by java compiler. Now say, you have a class named Contact with some variable and methods. You can access the variable or method if the class is instantiated. Say, Contact c = new Contact(). So now you can call c.someMethod() or c.someVariable. So before calling you need to instantiate using new keyword that happens in runtime. But if there any static block or static method or variable in that class, you don’t need to instantiate that class. Simply you can call it using Contact.someStaticMethod(). Now how these things happen? Good point. Any static block in any class is instantiated when the class is loaded to compile. So you need not to instantiate an object to call static block or variable, b/c it’s already instantiated.

how things are done actually, give a read, though, It may seem complicated to you.
http://onjava.com/pub/a/onjava/2005/01/26/classloading.html?page=1
BIT0112-Rokon
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

http://blog.codexplo.org

Back to top Go down

Java Main Method Empty Re: Java Main Method

Post by BIT0102-Mohaimin Sat Aug 20, 2011 2:29 pm

BIT0112-Rokon wrote:I have a question, Can the main( ) method be specified as private or protected? if, how?
I think, it should be, I have an idea how to do this. What do you think?

I think you can do that, but Java will not recognize the method as the starting point of the application. Not sure though.
BIT0102-Mohaimin
BIT0102-Mohaimin
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 415
Points : 715

Back to top Go down

Java Main Method Empty Re: Java Main Method

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum