Monday 14 January 2013

Why Public Static Void Main ? - Java Code

We are so much habitual now to begin our code with public static void main(). Because we were taught to do so. Being taught that this is the entry point for our java program to execute, however are we aware what does it actually means??
Plenty of other questions which came into my mind:
1. Why public static void main()
2. Whats the meaning of each one of the individual keyword?
3. What if I don't use it?
4. Is there any other way to start the program execution?
5. How does JVM knows, "i need to start from here..."?
6. Can we shuffle the keywords?
  and so on....

Well answering to each one of the above, lets begin.

We structure our complete code into classes and objects(instance of those classes), that's what java says to do. Now we write a program say xyz, create a public class for it XYZ and define a public static void main(String args[]) method in the class.

The first thing to know is public static void main () aka psvm() is a method signature with an Array of String type as an argument. The method signature is
[access modifier] [return type] method_name (Argument_List)

Secondly, its a must to use this signature because JVM looks for this signature to begin execution. If it is not able to find the above signature an exception is thrown. Note this is the only way to begin the program execution and nothing else would work.

public is an access modifier, which tells compiler that any class or object can access this method.
or simply, Access is granted to anybody. Making the main method public is necessary so that anyone (object) can access the method.

static.. Please remember in java we need an object reference to call any class method. I mean instantiating the class. Now do we have any object before beginning the program. No we don't. So how we are going to begin with. Static keyword before a field or method describes that no instance or object reference is required to access that field/mehtod().

void.. Simply means no return by the method
Note : there's a difference between null returned and void.

main.. As we all know, method name defined by java.
main is different from Main as java is case sensitive. Further main() is pre-defined mehtod where as you can create your own method Main() / Main(x) or anything.

Friday 4 January 2013

Thursday 3 January 2013

JavaScript runtime calculator

Close Browser Window (Tab) : JavaScript

Closing the opened tab through javaScript

Javascript:
run this

window.opener='x';
window.close();

Above example would ask for user confirmation.
Do you want to close or not ??? type


Incase you dont want confirmation from user... and force closing
Try this:

window.open('', '_self', '');
alert('Window is closing');
window.close();