Menu
Topics Index
...
`

Classes889


If you need explanation Read this topic

If you need Answer Take test on this topic

Classes889
What will be the output of the following program?
class OutPut
{
    int D;

    public static void main(String[] args)
    {
        PrintA a = new PrintA();
        PrintB c = new PrintB();

        System.out.println("printA = " + a.printA);
        System.out.println("printB = " + c.printB);

        System.out.println("D = " + D);
    }
}

class PrintA
{
    int printA;
}

class PrintB
{
    int printB = 5;
}

A. printA = 0
printB = 5
D = 0
B. printA = 0
printB = 5
D =(some object reference value)
C. Compilation Error
D. Runtime Error
Topic: Static and Non Static Variables - Static and Non Static Methods

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Ans is C...here we have 2 classes PrintA and PrintB and these classes have printA and printB as instance variables in it...
instance variables are class level variables these can use any where in the class except in static methods..

Coming to the main class here we have d as instance variable..in main() we create the instances for the class PrintA n PrintB so memory will allocate for the instance variables..next we are printing the values of printA and PrintB so it will print 0 default values...
next we are printing the value of D which is non static it Bcz main() static method so we cant use non static variable directly in static method...we can pass to a an static method..

Posted by Uday Kumar    2014-12-15 12:55:42


This dose is now closed and the winners are Sai Ram,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Asma Mujtaba Khan. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2014-12-16 04:45:29


© meritcampus 2019

All Rights Reserved.

Open In App