We declare, main method in java as : public static void main(String args[])
public : means it can be accessible to all other classes.
static : main is the entry point of a class. In java everything thing is written in a class.Now when you run java filename on command prompt, loader will load the class and jvm will search the main method to enter into the class. so making the main() as static, will make jvm access it directly through classname.main()
Now once jvm get the main(), object instantiated there are accessed.
void : main method should return anything.
main : its the method name, the JVM will seek for after the class is loaded on runing command like e.g java classname
String args[] : String array named args used to access the string variables passed on command line. e.g java classname firstname lastname
For further reading:
http://www.dreamincode.net/forums/index.php?s=eac3b49d45a9277f36a31b233588f6a5&showtopic=38921
just i want to know what is the exact meaning of the factory methods
factory nethod is a helper method
Only reason main is static because it has to be called without creating instance of class. see Why main is static public in java for complete details
I just want to know that what is the importance of static keyword in java??????Plz help me for this answer
There will be times when you will want to define a class member that will be used independently of any object of that class. Normally a class member must be accessed only in conjunction with an object of its class. However, it is possible to create a member that can be used by itself, without reference to a specific instance. To create such a member, precede its declaration with the keyword static. When a member is declared static, it can be accessed before any objects of its class are created, and without reference to any object. You can declare both methods and variables to be static. The most common example of a static member is main( ). main( ) is declared as static
because it must be called before any objects exist.
Instance variables declared as static are, essentially, global variables. When objects of its class are declared, no copy of a static variable is made. Instead, all instances of the class share the same static variable.
Methods declared as static have several restrictions:
They can only call other static methods.
They must only access static data.
They cannot refer to this or super in any way. (The keyword super relates to
inheritance.)
If you need to do computation in order to initialize your static variables, you can declare a static block which gets executed exactly once, when the class is first loaded. The following example shows a class that has a static method, some static variables, and a static initialization block:
// Demonstrate static variables, methods, and blocks.
class UseStatic {
static int a = 3;
static int b;
static void meth(int x) {
System.out.println(“x = ” + x);
System.out.println(“a = ” + a);
System.out.println(“b = ” + b);
}
static {
System.out.println(“Static block initialized.”);
b = a * 4;
}
public static void main(String args[]) {
meth(42);
}
}
As soon as the UseStatic class is loaded, all of the static statements are run. First, a is set to 3, then the static block executes (printing a message), and finally, b is initialized to a * 4 or 12. Then main( ) is called, which calls meth( ), passing 42 to x. The three println( ) statements refer to the two static variables a and b, as well as to the local variable x.
Note It is illegal to refer to any instance variables inside of a static method. Here is the output of the program:
Static block initialized.
x = 42
a = 3
b = 12
Outside of the class in which they are defined, static methods and variables can be used independently of any object. To do so, you need only specify the name of their class followed by the dot operator. For example, if you wish to call a static method from outside its class, you can do so using the following general form:
classname.method( )
Here, classname is the name of the class in which the static method is declared. As you can see, this format is similar to that used to call non-static methods through object reference variables. A static variable can be accessed in the same way—by use of the dot operator on the name of the class. This is how Java implements a controlled version of global functions and global variables.
Here is an example. Inside main( ), the static method callme( ) and the static variable b are accessed outside of their class.
class StaticDemo {
static int a = 42;
static int b = 99;
static void callme() {
System.out.println(“a = ” + a);
}
}
class StaticByName {
public static void main(String args[]) {
StaticDemo.callme();
System.out.println(“b = ” + StaticDemo.b);
}
}
Here is the output of this program:
a = 42
b = 99
Excellent, I like this answer.
thanks
i want to know why is method main declared static?
A method which reterns the same class object is factory method
great answer! even great to copy paste for homework!
nice answer
I m happy to get this answer
but it is not perfect answer….
Very good answer.
I just want to know that what is the importance of static keyword in java??????Plz help me for this answer????
the main method is declare as static bcoz there is no need to call static method and it’s info is contained by JVM. so method is declare static so that it can be called automatically.
good
what is basic diff b/w abstact method and interface?
interface is a collection of abstract methods. an abstract is something that will look like this……
public double getArea()
{
}
where as an interface is a collection of such methods.
nice answer….
why in java using static keyword before main
nice answer
nice answer thanx…
nice
orxjunkkuqzg
xakvtgnqnhtp
tpbzqsojyfiu
bjdfxmvpfrvg
xboaoiibiutj
bytznyyguubw
ulkxkfxppfij
mwumyeffqdqk
7
qpyblgxwlvgy
edokmlfwpupf
xyfqjsecsgwx
ucgjeqwvchpj
vimlarhxtsoy
bdekrujcaahn
hziqrmxzktor
wxzidndfnyvi
xuhajyycgauc
teybiwwuztol
iqpzfkdsvtep
qjxvabpuywij
abhhrbsofcxj
dufcfwvqfyoq
annjyaapgbsx
siyntpbjrbyg
syfwdsjxfsuq
dcjshnxvjgcm
bmspuptrcxsr
wsuysshywtfq
xgmcrbjtzegt
vrbcqoehowdj
ulyqekrhaszu
hrpeyjemulnj
qvsbcmqkfmih
eqncvlibhmhy
igqtandpvkfy
lxmntoigpwtl
mapvskeuveph
uddpvlsgcjer
aaupqcgyhhyx
ivbiuuulfzec
mfvvrlsmsgcd
iddqznxavckg
aakxynbfhcav
bccjdlmunyqz
cnkdfnjmslfw
yadnzakoahnh
rlpiqivljtdx