Static Keyword in java

Write a program in java to demonstrate the Static variable, Static Method, Static Block

 
public class StaticDemo 
{
 static int count=0;
 int sr;
 StaticDemo()
 {
  count++;
  sr=count;
 }
 void showsr()
 {
  System.out.println("Sr. No. of object created: "+sr);
 }
 static void showcount()
 {
  System.out.println("Total No. of object created: "+count);
 }
 public static void main(String[] args) 
        {
  StaticDemo.showcount();
  StaticDemo obj1=new StaticDemo();
  StaticDemo obj2=new StaticDemo();
  obj1.showsr();
  StaticDemo obj3=new StaticDemo();
  obj2.showsr();
  obj3.showsr();
  showcount();
 }
}

Output

3 comments:

  1. Thanks mate. I am really impressed with your writing talents and also with the layout on your weblog. Appreciate, Is this a paid subject matter or did you customize it yourself? Either way keep up the nice quality writing, it is rare to peer a nice weblog like this one nowadays. Thank you, check also event marketing and How 5G Will Impact the Future of Events Industry

    ReplyDelete
  2. This is an informative post. Got a lot of info and details from here. Thank you for sharing this and looking forward to reading more of your post.
    tax return for self employment in barking

    ReplyDelete