Hi Java Lovers, I am the static keyword in JAVA. I am identified as a access modifier in JAVA. I am used when programmers want to define something global in their class.
I can make any member of the class available to programmers even before they create objects of it. And later when they create many objects that variable is shared among all.
Do you think I am kiddin you....Obviously not. I do have special powers!!!
Read on.....
I can associate my self with quite a few things like
Variables: I make variables available to all the objects of the class. The variable can then be accessed by CLASSNAME.VARIABLENAME. There is no harm using (new CLASSNAME()). VARIABLENAME but does not make sense
public class ExplainStatic {
static int foo=10;
public void bar() {
foo = 30;
}
public static void main(String[] args) {
System.out.println("No objects of class created " +
"and yet foo exists : foo - " +Main.foo);
ExplainStatic object1 = new ExplainStatic ();
ExplainStatic object2 = new ExplainStatic ();
main1.bar();
System.out.println("main1 changed the value of foo but " +
"what is this it affected the foo " +
"of main2 also : foo - "+main2.foo);
}
}
Ouput :
No objects of class created and yet foo exists : foo - 10
main1 changed the value of foo but what is this it affected the foo of main2 also : foo - 30
Methods: When I get hooked on with a method ( make it a static method which can be called with the Class name itself. We don't really need an objects to call such a method. A BIG example of such a method is the main method [public static void main (String args]. Because of me (and little credit to my cousin public) only the JVM is able to to call the main method even before creating any instance of any class.
But I do bring some restrictions to the method also.
static int foo=10;
static void bar()
{
System.out.println("In static bar : foo - "+ foo);
foo = 40;
}
public static void main(String[] args)
{
System.out.println("In main : foo - "+ Main.foo);
Main.bar();
System.out.println("In main : foo - "+ Main.foo);
}
}
Output :
In main : foo - 10
In static bar : foo - 10
In main : foo - 40
Static blocks : I allow programmers to create block which execute while class is first loaded. These blocks are executed in the order they are present in the class and is allowed to access other static variables and static methods.
public class ExplainStatic{
static int foo=10;
static {
System.out.println("In static block : foo - "+foo);
foo = 20;
bar();
}
static void bar()
{
System.out.println("In static bar : foo - "+ foo);
foo = 40;
}
public static void main(String[] args)
{
System.out.println("In main : foo - "+ ExplainStatic.foo);
}
}
Output :
In static block : foo - 10
Nested Class : When I am added to a nested class I make sure that the nested class is not able to access the outer's class members without an object. But because of this I do make static nested class useless :(
public class ExplainStatic{
int foo=10;
void bar()
{
System.out.println(" ExplainStatic : foo - "+ foo);
}
static class ExplainStatic Inner
{
void foo()
{
System.out.println(" ExplainStatic Inner : foo");
ExplainStatic main = new ExplainStatic ();
main.bar();
}
}
public static void main(String[] args)
{
ExplainStatic Inner mainInner = new ExplainStatic . ExplainStatic ();
mainInner.foo();
}
}
Output:
Main Inner : foo
Main : foo - 10
I can make any member of the class available to programmers even before they create objects of it. And later when they create many objects that variable is shared among all.
Do you think I am kiddin you....Obviously not. I do have special powers!!!
Read on.....
I can associate my self with quite a few things like
- Class variables
- Methods
- Blocks
- Nested class
Variables: I make variables available to all the objects of the class. The variable can then be accessed by CLASSNAME.VARIABLENAME. There is no harm using (new CLASSNAME()). VARIABLENAME but does not make sense
public class ExplainStatic {
static int foo=10;
public void bar() {
foo = 30;
}
public static void main(String[] args) {
System.out.println("No objects of class created " +
"and yet foo exists : foo - " +Main.foo);
ExplainStatic object1 = new ExplainStatic ();
ExplainStatic object2 = new ExplainStatic ();
main1.bar();
System.out.println("main1 changed the value of foo but " +
"what is this it affected the foo " +
"of main2 also : foo - "+main2.foo);
}
}
No objects of class created and yet foo exists : foo - 10
main1 changed the value of foo but what is this it affected the foo of main2 also : foo - 30
Methods: When I get hooked on with a method ( make it a static method which can be called with the Class name itself. We don't really need an objects to call such a method. A BIG example of such a method is the main method [public static void main (String args]. Because of me (and little credit to my cousin public) only the JVM is able to to call the main method even before creating any instance of any class.
But I do bring some restrictions to the method also.
- I make a method capable of calling only other static methods and variables (or rather incapable of calling other non-static methods and variables, but dont tell anyone)
- Because of me the method does not get a reference of this and super. ()
static int foo=10;
static void bar()
{
System.out.println("In static bar : foo - "+ foo);
foo = 40;
}
public static void main(String[] args)
{
System.out.println("In main : foo - "+ Main.foo);
Main.bar();
System.out.println("In main : foo - "+ Main.foo);
}
}
Output :
In main : foo - 10
In static bar : foo - 10
In main : foo - 40
public class ExplainStatic{
static int foo=10;
static {
System.out.println("In static block : foo - "+foo);
foo = 20;
bar();
}
static void bar()
{
System.out.println("In static bar : foo - "+ foo);
foo = 40;
}
public static void main(String[] args)
{
System.out.println("In main : foo - "+ ExplainStatic.foo);
}
}
In static block : foo - 10
In static bar : foo - 20
In main : foo - 40public class ExplainStatic{
int foo=10;
void bar()
{
System.out.println(" ExplainStatic : foo - "+ foo);
}
static class ExplainStatic Inner
{
void foo()
{
System.out.println(" ExplainStatic Inner : foo");
ExplainStatic main = new ExplainStatic ();
main.bar();
}
}
public static void main(String[] args)
{
ExplainStatic Inner mainInner = new ExplainStatic . ExplainStatic ();
mainInner.foo();
}
}
Output:
Main Inner : foo
Main : foo - 10
No comments:
Post a Comment