Java codes on static

Previous
Next
public class Pro
{
    static int a=10;
    public static void main(String args[])
    {
        int a=20;
        
        Pro.a = a + Pro.a ;
        
        a = Pro.a + a ;
        System.out.println(a + Pro.a);
    }
}
public class Pro
{
    static int a=10;
    public static void main(String args[])
    {

        Pro.a = a + Pro.a ;
        
        a = Pro.a + a ;
        System.out.println(a + Pro.a);
    }
}
public class Pro
{
    static int a;
    public static void main(String args[])
    {
        System.out.println(a + Pro.a);
    }
    static
    {
        System.out.println(a);
        Pro.a = 100;
    }
}

Write the output:

public class Pro
{
    static int a;
    static
    {
        Pro.test(10);
    }
    public static void main(String []args)
    {
       System.out.println(Pro.a);
    }
     
    static void test(int a)
    {
       System.out.println(Pro.a);
       Pro.a = a;
    }
}

Output it:

public class Pro
{
    public static void main(String []args)
    {
       Pro.add(10,20);
       Pro.add(34,67);
       Pro.add(-23, 45);
    }
     
    static void add(int a, int b)
    {
       System.out.println("Sum is : " + (a+b));
    }
}
  • Any object(class) type variable holds address of object.
  • Pointer variables can hold address.
  • Pointer default value is null, hence any class type variable default value is null.
public class Pro
{
    static Sample s;
    static Demo d;
    static Test t;
    static String ss;
    public static void main(String []args)
    {
       System.out.println(Pro.s);
       System.out.println(Pro.d);
       System.out.println(Pro.t);
       System.out.println(Pro.ss);
    }
}
Previous
Next

Add Comment

Courses Enquiry Form