Loading...

Java Naming

Give a number a clear name so you can use the same spelling again. In Yard.java, store int ticketCount = 4; and print ticketCount. Compile with javac Yard.java and run java Yard. The screen should show 4. A second demo stores 4 under ticketCount and 9 under TicketCount, then prints both names. The screen should show 4, then 9, because those spellings are different names in Java. Leave formatting rules for the next page — this lesson is exact naming, not spacing style.

Store one clear name

The Javadoc page labeled a method contract. This page chooses readable names for values you store. Open Yard.java. Keep the public class name Yard. Delete leftover add demos if they bury the naming story, leftover println debug labels that only hunt a wrong sum, leftover equals demos, and any keyboard Scanner. The program must not wait for typing. No import line is required.
Inside Yard.main, write one name in camelCase — start with a lowercase letter, then capitalize later words in the same name:
int ticketCount = 4;
System.out.println(ticketCount);
The spelling ticketCount is the name. The value 4 sits under that spelling. When you print the name, Java looks up that exact spelling and writes the stored number.

Yard ticketCount holds 4

java
public class Yard {
    public static void main(String[] args) {
        int ticketCount = 4;
        System.out.println(ticketCount);
    }
}

From the folder that holds Yard.java, run javac Yard.java. When parentheses, braces, and semicolons match, javac writes no line of its own. Then run java Yard — not java Yard.java. Leave the .java ending off the run command. Keep one public class in the file so the name matches Yard.
The screen shows 4. Walk the run once. Java starts at Yard.main. The first line stores four under ticketCount. The print line looks up ticketCount and writes 4. No second name appears in this first demo. The lookup is exact. TicketCount is not the name you stored in this first program.
Say the beginner rule while you watch the screen: pick one spelling, store the value, ask for the same spelling. Do not invent long formatting lectures on this page. Formatting waits for the next lesson.
If you still see 12 from a Javadoc add demo, switch to the naming teaching file and run java Yard again. If you write ticket count = 4 with a space in the middle, that is not one name. Keep the camelCase form as a single word.

A capital letter makes a second name

A second program keeps ticketCount and adds another name that differs only by one capital letter. Java treats ticketCount and TicketCount as two names. Store 4 under the first spelling and 9 under the second, then print both.

Yard ticketCount and TicketCount

java
public class Yard {
    public static void main(String[] args) {
        int ticketCount = 4;
        int TicketCount = 9;
        System.out.println(ticketCount);
        System.out.println(TicketCount);
    }
}

Compile with javac Yard.java, then run java Yard. The screen shows 4, then 9. The first print reads ticketCount. The second print reads TicketCount. Those are not the same lookup. Changing one letter’s case creates a new name. Beginners often expect the second spelling to update the first. On this page it does not. Each spelling keeps its own value.
Read the two declarations once more. The class name Yard stays PascalCase with a capital first letter — that matches the file name. The value names in main show the contrast: camelCase ticketCount for a normal variable, and a deliberately different TicketCount only to prove case sensitivity. In everyday code you still prefer camelCase for variables; the second name exists here as a teaching contrast, not as a style you should copy everywhere.
If leftover Javadoc demos still sit nearby, keep them as history. For this lesson run the naming teaching file so the screen shows named counts, not only a documented add result. If javac complains about duplicate public classes, keep only Yard public.

Names you should not start

A name should not start with a digit. A line like int 4tickets = 4; is not in these demos on purpose. That shape is illegal in Java, so javac rejects it before the program can run. Keep the first character a letter or an underscore in real work. This page’s working demos start with t or T.
A space inside a name also fails. ticket count is two tokens, not one name. CamelCase joins the words without spaces: ticketCount. Keep that habit when you name the next variable on your own.
You do not need a table on this page. The two screens are the map: one name prints 4; two differently capitalized names print 4 then 9.

Compared with Javadoc labels

On the Javadoc page, @param and @return labeled a method’s contract. On this page, the spelling of the variable is the contract for looking the value up again. You still compile Yard.java and run java Yard. The new habit is choosing a clear camelCase name and respecting exact case when you read it back.
Javadoc comments do not replace good names. A clear name and a clear comment can sit together later. This lesson isolates naming so the screen proves case-sensitive lookup without mixing documentation tags into the demo.
If a leftover add file is still open, you may see 12 only. Switch to the naming file so main stores and prints ticketCount. Keep formatting style guides off this page. Keep keyboard input off this page.

Before formatting

You can store ticketCount as 4 and see 4 on the screen. You can add TicketCount as 9 and see 4 then 9. Compile Yard.java and run java Yard for each kept demo in turn. The spelling decided which value appeared. The next page is formatting, where you tidy how code is spaced and arranged for readers.
Keep the single-name demo for a clear first print. Keep the two-name demo for a clear case contrast. Keep formatting files off this page until that lesson. Those three habits are the checkpoint before you move on. When the same spelling returns the value you stored — and a different capital letter does not steal that value — naming did its job for this lesson and you are ready for the next topic about formatting.
One last pass through the folder: one public Yard, clear names in main, no keyboard wait, no forced formatting rules. Run javac Yard.java and java Yard once more on the demo you keep open. If the screen still matches the names you typed, the naming lesson is solid and the course can move to formatting on the next page. Prefer one naming idea per run so each screen teaches one contrast.

🧠 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