Java Programming - Declarations and Access Control - Discussion

Discussion Forum : Declarations and Access Control - Finding the output (Q.No. 5)
5.
What will be the output of the program?
public class Test 
{
    public int aMethod()
    {
        static int i = 0;
        i++;
        return i;
    }
    public static void main(String args[])
    {
        Test test = new Test();
        test.aMethod();
        int j = test.aMethod();
        System.out.println(j);
    }
}
0
1
2
Compilation fails.
Answer: Option
Explanation:

Compilation failed because static was an illegal start of expression - method variables do not have a modifier (they are always considered local).

Discussion:
14 comments Page 2 of 2.

Bhargavi said:   1 decade ago
Static variables cannot be instantiated and it could be accessed only by class-name.

Vinodkumar said:   1 decade ago
When we use static it binds with class but there we are using static inside of non-static method so it won't bind with class because we got compilation error.

Archana said:   1 decade ago
We cannot access non-static methods in static method.

Vimmi said:   1 decade ago
There are so many answers here but the correct is Static variables cannot be local variable in java.


Post your comments here:

Your comments will be displayed after verification.