Wednesday, June 2, 2021

JAVA program to illustrate the i) static block ii) static variable iii) static method iv) final keywords.

PROGRAM :

// Static Block

package com.company;

class staticblock

{

    static int num;

    static

    {

        num=40;

        System.out.println("Static Block");

    }

    public static void main(String[] args)

    {

        System.out.println(" Value of num="+num);

    }

}

OUTPUT :




PROGRAM :

// Static Data

package com.company;

class staticeg1

{

    int a;

    static int b;

    void getData(int m)

    {

        a=m;

        b++;

    }

    void putData()

    {

        System.out.println( a);

        System.out.println( b);

    }

}

class staticdata

{

    public static void main(String[] args)

    {

        staticeg1 ob1=new staticeg1();

        staticeg1 ob2=new staticeg1();

        ob1.getData(10); ob2.getData(20);

        ob1.putData(); ob2.putData();

    }

}


OUTPUT :


PROGRAM :

// static method

package com.company;

class staticeg2{

    int a;

    static int b;

    void getData(int m)

    {

        a=m;    b++;

    }

    static void printData()

    {

        System.out.println( b);

    }

 

    void putData()

    {

        System.out.println( a);

        System.out.println( b);

    }

}

class staticmethod

{

    public static void main(String[] args)

    {

        staticeg2.printData();

        staticeg2 ob1=new staticeg2();

        staticeg2 ob2=new staticeg2();

        ob1.getData(10); ob2.getData(20);

        ob1.putData(); ob2.putData();

    }

}

OUTPUT :



PROGRAM :

// final variable

package com.company;

class A

{

    int a=10;

    final int b=20;

    void change()

    {

        a=a+10;

        System.out.println(a);

        System.out.println(b);

    }

}

class finalvar

{

        public static void main(String[] args)

        {

            A ob=new A();

            ob.change();

        }

}

OUTPUT :



PROGRAM :

// final method

package com.company;

class A

{

    void display()

    {

        System.out.println("display of super method");

    }

    final void show( )

    {

        System.out.println(" Show() implemented");

    }

}

class finalmethod extends A

{

    void display()

    {

        System.out.println("display of sub method");

    }

     /* void show( )

         {

                 System.out.println(" Show() implemented");

          }

                     cant be inherited

       */

}


PROGRAM :

// final class

package com.company;

//A final class can’t be inherited

final class finalclass

{

    void display()

    {

        System.out.println("Hello");

    }

}

/*final class A can’t be inherited

class B extends A

{

       void show( )

{

        System.out.println(" Show() implemented");

}

}

 A final class can’t be inherited

*/

Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home