What will be the output of the following Java code?

 class access
   {
       public int x;
	private int y;
       void cal(int a, int b)
       {
           x =  a + 1;
           y =  b;
       }        
   }    
   public class access_specifier 
   {
       public static void main(String args[])
       {
           access obj = new access();   
           obj.cal(2, 3);
           System.out.println(obj.x + " " + obj.y);     
       }
  }

a) 3 3
b) 2 3
c) Runtime Error
d) Compilation Error

1 thought on “What will be the output of the following Java code?”

  1. c
    Explanation: None.
    output:
    $ javac access_specifier.java
    Exception in thread “main” java.lang.Error: Unresolved compilation problem:
    The field access.y is not visible

Leave a Comment