Loading...

Java Access

Write a class Gate in Gate.java with a private int field code, a constructor Gate(int n) that sets code = n;, and a method void show() that prints code. In Pier.java, build one gate with Gate g = new Gate(9);, then call g.show();. Compile both files and run java Pier. The screen should show one line: 9. The field is hidden from Pier. The method on Gate can still read it. Using private on the field is what this page teaches.

A field Pier should not name directly

Until now other classes in the same folder could often reach fields like j.fill or t.size when those fields had no access word. The word private changes that. A private field belongs to the class that declares it. Methods inside that same class may use the field. A different class — here Pier — should not write g.code.
Create Gate.java first. The public class line is public class Gate. Do not leave an older Tag or Jar class name here. Inside the braces declare private int code;. Write the constructor Gate(int n) { code = n; } with different names so this page does not need this. Then write:
void show() { System.out.println(code); }
There is no main in Gate.java. Save before you compile. The method show sits in the same class as code, so it may read the private field. That is the allowed path on this page.

Gate with private code

java
public class Gate {
    private int code;

    Gate(int n) {
        code = n;
    }

    void show() {
        System.out.println(code);
    }
}

Pier calls show, not g.code

Create Pier.java in the same folder. Its public class line is public class Pier. Inside main, write Gate g = new Gate(9); then g.show();. Do not write System.out.println(g.code); on this page. If you try g.code from Pier while code is private, javac refuses.
Keep Pier as the class that holds main. Do not put main inside Gate for this lesson. If a leftover Yard file from the static page still sits in the folder, do not run java Yard for this lesson — run java Pier after you compile Gate.java and Pier.java.

Pier calls show

java
public class Pier {
    public static void main(String[] args) {
        Gate g = new Gate(9);
        g.show();
    }
}

From the folder that holds both files, run javac Gate.java Pier.java. You can compile Gate.java first and Pier.java second when both are complete. When parentheses, braces, and semicolons match, javac writes no line of its own. Then run java Pier — not java Pier.java, and not java Gate. Leave the .java ending off the run command. Start the class that holds main. The same commands work on Windows and on macOS.
The program should print one line and stop: 9. It should not ask you to type. If the terminal waits for keyboard input, a nextLine call from an older exercise is still in a file. Delete that call and keep new Gate(9) plus g.show(); in Pier, with private int code in Gate.
Walk the run once. Java starts at Pier.main. The expression new Gate(9) builds a gate and stores 9 in code inside the constructor. The call g.show() enters show on that same object. Inside Gate, show may read code and prints 9. Pier never named the field. The screen shows 9.
If you write System.out.println(g.code); in Pier instead of g.show();, javac stops with an access error while code is private. That refusal is the point of this page. Put g.show(); back.

Compared with static and with open fields

On the static page you called Tag.ping(); with no object field to hide. That still works. This page hides a field and opens a method. A leftover Tag.ping(); demo is a different lesson. For this page keep Gate and Pier and g.show();.
Earlier pages often left fields without private, so j.fill from another class compiled. That looser shape still appears in small demos. This lesson chooses private on purpose so you see the boundary. Printing g alone with System.out.println(g); does not show a clean 9. Call g.show(); or print through a method that the class allows.

Two gates, two codes

Each new Gate(...) keeps its own private code. You can write Gate a = new Gate(2); and Gate b = new Gate(8);, then call a.show(); and b.show();.

Pier shows two gates

java
public class Pier {
    public static void main(String[] args) {
        Gate a = new Gate(2);
        Gate b = new Gate(8);
        a.show();
        b.show();
    }
}

Compile and run again: javac Gate.java Pier.java, then java Pier. The screen should show 2 on one line and 8 on the next. Each show call ran on its own object and read that object’s private field. Writing Gate b = a; would not build a second object — use two new Gate(...) calls when you want two codes.
If both lines show the same number, check that each new passes a different argument. If javac complains about code in Pier, you still have a direct g.code line — remove it and keep show.

What this leaves for packages

This page keeps both files in the same folder with default package layout. A later page — packages — puts classes into named folders with package lines. Stay with private and show here. Do not invent a full public/protected chart on this page; the lesson goal is one private field and one method that still can read it.
If javac cannot find Gate while compiling Pier.java, check that Gate.java sits in the same folder and that the class line is public class Gate. When the folder still holds Tag.java or Yard.java, leave them alone or delete them only if they confuse your compile command. Run java Pier after you compile the Gate and Pier pair from this lesson.

Before packages

You can declare private int code;, write Gate(int n) { code = n; } and void show() { System.out.println(code); }, and from Pier run Gate g = new Gate(9); then g.show();. Compile both files and run java Pier. The screen shows 9. The private field stayed inside Gate. The method on the same class printed it. The next page is packages, where you place classes into named package folders.
Keep private on the field. Keep the print inside show, not as g.code from Pier. Keep main in Pier. Those three habits are the checkpoint for this page before you move on. When the screen shows 9 through show, you have crossed the access boundary the way this lesson intends.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
5
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 Instructions

  • Read each question carefully
  • Select the best answer for each question
  • You can retake the quiz as many times as you want
  • Your progress will be shown at the top