Hi,
I have seen that on page mentioned below for static keyword explanation given is: Page: http://www.roseindia.net/java/learn-java-in-a-day/create-first-program.shtml Explanation: static keyword indicates that this method can be invoked simply by using the name of the class without creating any class-object.
Can you I get and an simple example for this?
The static variable is declared by using static keyword. Following is an example of static declaration:
static int i=10;
The variable declared with static keyword is called class variable. This variable is shared by all the instances of the class. The static variable can be accessed without creating the instance of the class or you can say, it can be called without any reference.
This variable is created statically and same variable is accessed by all instance of the class.
Example of static keyword:
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); } }
In the above code, 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
The static variable is declared by using static keyword. Following is an example of static declaration:
static int i=10;
The variable declared with static keyword is called class variable. This variable is shared by all the instances of the class. The static variable can be accessed without creating the instance of the class or you can say, it can be called without any reference.
This variable is created statically and same variable is accessed by all instance of the class.
Example of static keyword:
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); } }
In the above code, 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
Ads