Java Main Method
4 posters
Page 1 of 1
Java Main Method
Several times junior mates at IIT, who just started Java, asked me to explain the signature of the Java main method.
The signature:
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:
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,
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.
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- Programmer
- Course(s) :
- BIT
Blood Group : B+
Posts : 415
Points : 715
Re: Java Main Method
Nice one
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
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
Re: Java Main Method
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- Programmer
- Course(s) :
- BIT
Blood Group : B+
Posts : 415
Points : 715
Re: Java Main Method
Hmm.... then maybe you should explain in the later part of title post that... they should use args
Re: Java Main Method
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, it should be, I have an idea how to do this. What do you think?
Re: Java Main Method
"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?
What does it mean?
BIT0322-Hira- Alpha Release
- Course(s) :
- BIT
Blood Group : O+
Posts : 24
Points : 33
Re: Java Main Method
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
Re: Java Main Method
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- Programmer
- Course(s) :
- BIT
Blood Group : B+
Posts : 415
Points : 715
Similar topics
» print hello world without main method in java
» printf method in Java
» Write a Swap method in Java
» Java Tutorial: Using Two Java Reference Keywords, this and super
» Newton-Raphson method in C
» printf method in Java
» Write a Swap method in Java
» Java Tutorial: Using Two Java Reference Keywords, this and super
» Newton-Raphson method in C
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|
Tue Sep 29, 2015 2:45 pm by Sophiawood
» Cisco EHWIC SFP/GE WAN Card
Mon Sep 07, 2015 11:08 am by Sophiawood
» Huawei S1700-28GFR-4P-AC Price
Thu Jun 25, 2015 2:31 pm by Sophiawood
» teach yourself C++ / Herbert Schildt Solutions
Wed Jun 03, 2015 1:52 pm by Abdullah Al Noman
» teach yourself c by herbert schildt pdf
Wed May 13, 2015 11:01 pm by Raquib Ridwan
» ASA 5506X With Firepower ASA5506-K9
Fri Apr 10, 2015 4:31 pm by Sophiawood
» New Trends in Deal Business
Tue Feb 03, 2015 9:38 pm by nersoa
» PoE Power Allocation for WS-C2960S-24PS-L
Wed Nov 05, 2014 11:12 am by Sophiawood
» How to cure back pain
Fri Oct 31, 2014 7:15 pm by Bergen Guildford