Search This Blog

If you like any posts in this blog then click this icon which is present under the post

Monday, 18 July 2011

Program to Demonstrate Static & this Keyword


     This program is used to demonstrate static variable, static member and this keyword.

import java.io.*;
import java.util.*;
class Employee
{
            String fn,ln;
            static int count=0; 
            Employee( String s,String s1)
            {

                        count++;
                        fn=s;
                        ln=s1;
            }
            String getfirst()
            {
                        return this.fn;
            }
            String getlast()
            {
                        return this.ln;
            }
            static void getcount()
            {
                        System.out.println("Actual Count: "+count);
            }
            protected void finalize()
            {
                        count--;
                        System.out.println("Dceremented count: "+count);
            }

}
class Mainemp
{
            public static void main(String args[])
            {                                             
                        Employee e=new Employee("JAVA","PROGRAM");
                        String h=e.getfirst();
                        String h1=e.getlast();
                        System.out.println(h+" "+h1);
                        e.getcount();
                        Employee e1=new Employee("JAVA1","PROGRAM1");
                        String h2=e1.getfirst();
                        String h3=e1.getlast();
                        System.out.println(h2+" "+h3);
                        e.getcount();
                        Employee e2=new Employee("JAVA2","PROGRAM2");
                        String h4=e2.getfirst();
                        String h5=e2.getlast();
                        System.out.println(h4+" "+h5);
                        e.getcount();
                        e=e1=e2=null;
                        System.gc();
            }
}

Output

JAVA PROGAM
Actual Count: 1
JAVA1 PROGAM1
Actual Count: 2
JAVA2 PROGAM2
Actual Count: 3
Dceremented count: 2
Dceremented count: 1
Dceremented count: 0


No comments:

Post a Comment

Followers