Great question! Java is strict with rules. Here's a quick checklist of the most important things you MUST remember while writing Java code—like semicolon, braces, and more.
✅ JAVA CODE RULES TO ALWAYS REMEMBER
1. Semicolon (;)
๐น Use it to end every statement.
๐ Remember: If you forget it, compiler will give an error.
int x = 10; // ✅ correct
System.out.println(x); // ✅ correct
2. Curly Braces { }
๐น Use for class, method, loops, if-else blocks.
๐ Open { and always close with }.
if (x > 0) {
System.out.println("Positive");
} // ✅ braces are matched
3. Main Method Syntax (fixed format)
๐น This is where your program starts running.
๐ Don’t change the spelling or structure.
public static void main(String[] args) {
// code
}
4. Class Name = File Name (Case Sensitive)
๐น File name must match the class name (if public).
public class Hello {
public static void main(String[] args) {
System.out.println("Hi");
}
}
// Must be saved as Hello.java ✅
5. Case Sensitive
๐น Hello ≠ hello ≠ HELLO
๐ Variable and method names are case-sensitive.
int num = 5;
System.out.println(num); // ✅
System.out.println(Num); // ❌ Error
6. Use new to Create Object
๐น You can't use a class directly—use new.
Student s = new Student(); // ✅
7. Use System.out.println() for Output
๐น Java’s standard way to print something.
System.out.println("Hello");
8. Always Close Quotes and Brackets
๐น Forgetting a " or ) causes errors.
System.out.println("Hi"); // ✅
System.out.println("Hi); // ❌ missing "
๐ง BONUS MEMORY HACK (CLUE WORDS)
| ๐ก What | ๐ง Clue | ✅ Tip |
|---|---|---|
| End statement | Semicolon ; |
Like a full stop . |
| Group code | Curly braces {} |
Open door → do stuff → close door |
| Output | System.out.println() |
Say something out loud |
| Create object | new |
"new object born" |
| Start program | main method |
Like main() in C/C++ |
| File name = class | Match exactly | If class Cat, file = Cat.java |
✅ PRACTICE CHECKLIST FOR EVERY JAVA PROGRAM
✔ Starts with class
✔ Has main method
✔ Ends each line with ;
✔ Uses {} correctly
✔ Uses System.out.println() for output
✔ Object created using new
✔ File name = public class name
If you want, I can give you a template to copy-paste every time. Want that?
Absolutely! Here's more detailed guidance on writing correct and clean Java code—beyond semicolons and syntax. This will help you avoid errors and write code like a pro.
๐ง PART 1: STRUCTURE TO REMEMBER
✅ Basic Java Program Template
// Class name (must match file name if public)
public class MyProgram {
// Main method (entry point)
public static void main(String[] args) {
// Your code here
System.out.println("Hello, Java!");
}
}
๐ก Tip: Save file as
MyProgram.java
๐ PART 2: COMMON MISTAKES & REMINDERS
1. Every statement ends with ;
int a = 5; // ✅
a = a + 10; // ✅
❌ Forgetting
;causes "expected ;" error
2. Curly braces {} must always be paired
if (a > 0) {
System.out.println("Positive");
} // ✅ closed
❌ Forgetting
}gives "reached end of file while parsing" error
3. Case-sensitive
int Age = 20;
System.out.println(Age); // ✅
System.out.println(age); // ❌ age not defined
4. Class name = File name (important!)
public class HelloWorld {}
// Must save as HelloWorld.java ✅
5. Use correct method names (spelling matters!)
System.out.println("Hello"); // ✅
System.out.print("No newline"); // ✅
❌
system.out.Println()will cause errors (capitalization matters)
6. Create objects with new
Car myCar = new Car(); // ✅
7. Avoid using public class more than once in same file
public class A {} // ✅
class B {} // ✅
❌ Only one public class per file
๐ง PART 3: TIPS TO WRITE CLEAN CODE
| ✅ DO | ❌ AVOID |
|---|---|
| Use indentation (tabs/spaces) | Writing all code in one line |
| Use meaningful variable names | Names like x1, var2 |
Use comments // to explain logic |
No comments makes code confusing |
Group related code with {} |
Writing code without structure |
๐ PART 4: Java Keyword Categories (Mini Reference)
๐น Data Types
int, float, double, char, boolean, String
๐น Control Flow
if, else, switch, while, for, break, continue
๐น OOP Basics
class, object, new, this, public, private, protected
๐น Error Handling
try, catch, throw, throws, finally
๐น Other
static, void, return, import, package
๐งช PART 5: MINI PRACTICE CHALLENGE
Try writing this yourself:
✅ Create a simple class Student with:
- Attributes:
name,roll - Method:
displayDetails()which prints both
Expected Output:
Name: Ravi
Roll: 101
Need help with the answer? I can give you the full code after you try!
Would you also like a Java cheat sheet PDF or a beginner Java roadmap next?
References
https://chatgpt.com/share/6882f1b3-e66c-8001-8cca-a4783ad31d32