Total Pageviews

Thursday, January 20, 2011

Static Keyword

Static Keyword

Static keyowrd can be applied to a member function or a member field of a class. Static is one of the most interesting keywords in java. Staticvariables/methods are class variables/methods. By class variables i mean that if 10 instances are created for a class then each object has its own value for the fields other than the static fields. Static fields are comman to all the objects ie if one objects modifies the value,it is reflected in all other objects.
To access a static method/filed always use CLASSNAME.fieldname/methodname. Though the same can be accessed using object name also but this approach is strictly not recommended,as it adds to confusion.


 We declare methods as static when there is no need for an implicit parameter( By imp  licit i mean is the object name ie "this" in java). So methods can be directly called. Static blocks are  initialsed as soon as the class is lodaed.
  They are called before the constructor is created. They remain  in memory even after the constructor is removed.


Try the following code:

public class TestStatic{

static{
sout("hello i am not in main method");
}

public static void main(String [] args){
sout("in main method");
}

Output is
hello i am not in main method
in main method


Ok here are some excercise for you. I request not to run it on system. Just think  over the possible outcome and comment upon the result. Lets discuss what the  output can be and why so. I beleive i can get some active users for discussion :

1.Try out the above example by omiiting the main method and run the program using  command prompt. It yields interesting result.
2. Try creating a static  variable in a static/non static method.
3. Return a static variable(both mutable and immutable) to a non static variable.  Change the value of the non static variable and print the returned variable( using getter method of the class). See whether the value changes or not.
4. Output of the program :
public class LearnJava{
public static void main(String[] args){
 System.out.println("in main");
}

Static{
                System.out.println(" out of  hello");
}

 static{
 System.out.println("hello");
}
}

5. Output of the program :

public class LearnJava{
static{
 System.out.println(" out of  hello");
}
public static void main(String[] args){
 System.out.println("in main");
}

 static{
 System.out.println("hello");
}
}


6. WAP to distibute 8 cards to 1 user from a deck of 52 playing cards.
7. WAP to distribute all 52 cards amongst 4 users by each player picking one card  randomnly at a time.(For simplicity it can be assumed that cards have nymber  form 1 to 52 for both questions) HINT: Create 4 objects of the Cards class.

PS: There is a lot more which can be covered in this article. Please post as much as you can.


2 comments:

  1. Q4 & Q5 are straight forward. All the static blocks are executed before the main method in the order they appear in the programme.

    So output will be
    -->
    out of hello
    hello
    in main

    ReplyDelete
  2. Well lets just wait for others to answer.

    ReplyDelete