Write program to run a class, using object.

Program:

//define class A, with no main method
class A
{
    // define variable data types
    int a=5;
    char m =97;
   
    //define display method
    void display()
    {
        // define variable data types
        int k=15;
        k=25;
        k=35;
        //calling print function
        System.out.println(a);
        System.out.println(a);
        System.out.println(a);
        System.out.println(m);
        System.out.println(k);
        System.out.println(this.a);
        //System.out.println(this.k);
    }
}

//define class B, with main method
class B
{
    // define main method from where the execution of program begin
    public static void main(String args[])
    {
        //create object b of class A
        A b=new A();
        //call display method with object
        b.display();
    }
}



Sample output:

Post a Comment

Previous Post Next Post