Skip to main content

Access Modifiers

Access Modifiers | Java: Access Modifiers are controllers of data of variables, methods and classes. Those are four default, public, protected and private.

Syntax:                                        access_madifier int x;

Here is the Code:

  1. import java.io.*;
  2. class Main {
  3. public static void main (String[] args) {
  4. A a = new A(1);
  5. System.out.println(a.getX());
  6. }
  7. }
  8. class A 
  9. {
  10.     private int x;
  11.     A(int x)
  12.     {
  13.         this.x = x;
  14.     }
  15.     public int getX()
  16.     {
  17.         return this.x;
  18.     }
  19. }

Output:
1

Here is the Video: