Java Scope
Write a method named mark with one parameter: static void mark(int n). Inside the braces print that parameter with System.out.println(n);. From main call mark(4);. The screen should show one line: 4. The name n exists only inside mark. If you try to print n from main, javac refuses. That boundary — which names a method can see — is what this page teaches.
Names that stay inside a method
On the overloading page, several paint methods shared a name but kept separate parameter lists. Here the focus is smaller: one method, one parameter name, and where that name is allowed to appear. A parameter is a name that belongs to the method that declares it. Outside that method’s braces, the name is gone.
Create a small program file named Gate.java. The public class on the first line must match the file name: public class Gate. Do not leave an older Sign or Box class name in this file; the compiler expects the class name and the file name to agree.
Outside main, still inside the class braces, write static void mark(int n). The body has one print: System.out.println(n);. Keep void on this page. You are watching visibility, not return values. Save the file before you compile. The full first program is in the next box.
Gate.java with mark
This is the version you compile first. main holds the call. mark holds the print that uses n.
public class Gate {
public static void main(String[] args) {
mark(4);
}
static void mark(int n) {
System.out.println(n);
}
}
From the folder that holds Gate.java, run javac Gate.java. When parentheses, braces, and semicolons match, javac writes no line of its own. Then run java Gate — not java Gate.java. Leave the .java ending off the second command. The same two commands work on Windows and on macOS. The program should print one line and stop: 4. It should not ask you to type. If the terminal waits for keyboard input, a nextLine call from an older exercise is still in the file. Delete that call and keep mark(4); plus the mark method above.
Walk the run once in order. Java starts at main. The call mark(4) sends 4 into the parameter n. Inside mark, println(n) writes 4. When mark finishes, the name n is finished too. main never held a name n. The number 4 crossed the call as a value. The name n did not move into main.
If you add System.out.println(n); inside main after the call, javac reports that it cannot find the symbol n. That error is the lesson working: n was never declared in main. Fix by removing that line from main, or by declaring a different name in main if you need a local there. Do not expect the parameter from mark to appear in main by itself.
A local name next to the parameter
Inside mark you can also declare a new name that is not a parameter. That local name lives only between the braces of mark. Change the body so it stores a label and prints both the label and n on one line, or keep a second short method for the demo — on this page the next box shows passing a name from main instead, which is the usual beginner path.
A common beginner hope is that a name declared in main is automatically visible inside every method in the class. It is not, on this shape. If main has int room = 4; and mark tries to print room without receiving it, javac cannot find room inside mark. The fix is to pass the value: mark(room); with mark still declared as mark(int n).
Pass a main local into mark
Declare int room = 4; inside main. Call mark(room);. Inside mark, keep printing n. The screen still shows 4. The local name in main is room. The parameter name in mark is n. They are different names in different places. The value 4 is what travels.
public class Gate {
public static void main(String[] args) {
int room = 4;
mark(room);
}
static void mark(int n) {
System.out.println(n);
}
}
Compile and run again: javac Gate.java, then java Gate. You still see one line: 4. If you change room to 9 and leave the method body alone, the screen shows 9. The method still prints whatever value arrived in n. main can print room as well if you add System.out.println(room); in main — that name belongs there. main still cannot print n.
If mark declares its own local — for example int tag = 1; inside the braces — that tag is invisible to main. Trying System.out.println(tag); in main fails for the same reason printing n from main fails. The braces of mark are a wall for names declared inside them.
What the wall protects
Read the split in plain words. Names declared in main stay in main unless you pass their values into a call. Names declared in mark — parameters and locals — stay in mark. The call carries values, not the names themselves. Overloading from the previous page is a different skill: several methods with one shared method name. Scope is about where a variable or parameter name is allowed to appear in the source.
A leftover Sign demo with several paint methods can sit in the same file and confuse the screen if main still calls paint("ok"). Clear main down to the mark call you are testing. Do not nest mark inside main’s braces. Methods sit inside the class, next to main.
Writing System.out.println("n"); in main prints the letter n in quotes. It does not print the parameter. Keep println(n) inside mark when you want the value that arrived through the call. Writing mark(); with empty parentheses fails when the header still says mark(int n) — the call must send an int, whether a literal like 4 or a local like room.
Before recursion
You can write mark(int n), print n inside that method, and keep n out of main. You can declare room in main and pass room into mark. The screen shows the value that crossed the call. Names do not leak through the braces on their own. The next page is recursion, where a method calls itself with a smaller argument until a stop condition ends the chain.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 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