1. Mistakes when declaring a class
Missing access modifiers (package-private by default)
In Java, if you don’t explicitly set an access modifier on a class, it becomes accessible only within the current package. This is called package-private or “default access”. For example:
// File Book.java
class Book {
String title;
}
Such a class cannot be used from another package. If you want a class to be accessible everywhere (which is usually what you want for core models), use the public modifier:
public class Book {
String title;
}
Why is this important?
If you forget to mark it public, you’ll then spend a long time wondering why some Library from another package can’t see your class.
Class name and file name mismatch
In Java, the file name must match the name of the public class inside that file (case-sensitive!). For example:
// File Book.java
public class Book {
// ...
}
If you name the file book.java (lowercase) or, even worse, BooK.java, the compiler will report an error. This is one reason your IDE loves to complain when you rename a class but forget to rename the file.
Declaration syntax errors: missing braces, incorrect placement of methods and fields
Java is a strict language. If you forget a brace somewhere or accidentally declare a method inside another method (you can’t do that!), the compiler will immediately scold you.
Example of an incorrect declaration:
public class User
String name; // Oops! Class braces are missing
public void printName() {
System.out.println(name);
}
}
Correct version:
public class User {
String name;
public void printName() {
System.out.println(name);
}
}
Remember:
- All fields and methods are declared only within the class’s braces.
- Methods cannot be nested inside each other (some languages allow nested methods, but not Java!).
2. Mistakes when creating objects
Attempting to use an object before it’s initialized (NullPointerException)
The most common mistake of all time is trying to access an object that hasn’t been created yet. In Java, reference-type variables are set to null by default.
Example:
User user;
System.out.println(user.name); // NullPointerException!
Why?
The variable user is declared but not initialized. When you try to access a field or method, you’ll get an NPE (NullPointerException).
Correct approach:
User user = new User();
System.out.println(user.name); // Now it’s OK (if the field isn’t null)
Constructor call mistakes (parameter mismatch, missing no-arg constructor)
If a class only has a constructor with parameters, attempting to create an object without parameters will cause a compilation error.
public class Book {
String title;
public Book(String title) {
this.title = title;
}
}
Book book = new Book(); // Error! No no-arg constructor
Solution: either add a no-arg constructor, or always pass the required values:
Book book = new Book("Java for Dummies");
Creating objects repeatedly instead of reusing them
Sometimes beginners create a new object every time they need it instead of using the one they already created.
Book book = new Book("Java for Dummies");
System.out.println(book.title);
book = new Book("Java for Dummies"); // Why? You already had such an object!
System.out.println(book.title);
This isn’t a compilation error, but it’s poor style—you lose the reference to the old object and waste extra memory.
3. Best practices: how to avoid pitfalls
Always specify access modifiers explicitly
Don’t be lazy about writing public, private, or protected—it’s not only about safety but also about code readability. Your IDE will hint at it, of course, but it’s better to form the habit right away.
Example:
public class User {
private String name;
// ...
}
Mind your class naming
- Class names start with a capital letter, use CamelCase: Book, LibraryUser, BookManager.
- The file name must exactly match the class name.
Use constructors to initialize required fields
If an object has fields that must be set at creation time, add a constructor with parameters. This prevents mistakes at the usage stage.
Example:
public class User {
private String name;
public User(String name) {
this.name = name;
}
}
Now you can’t create a User without a name—the compiler won’t allow it.
4. Putting it into practice: build a mini‑app “Library”
Let’s continue developing our training app. Suppose we have a Book class and a Library class that manages books.
Example of a correct class declaration
// File Book.java
public class Book {
private String title;
private String author;
// Constructor with parameters
public Book(String title, String author) {
this.title = title;
this.author = author;
}
// Getters
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
}
Example of correct object creation
Book book = new Book("Java for Dummies", "John Doe");
System.out.println(book.getTitle() + " - " + book.getAuthor());
Example of a manager class
// File Library.java
import java.util.ArrayList;
public class Library {
private ArrayList<Book> books = new ArrayList<>();
public void addBook(Book book) {
books.add(book);
}
public void printAllBooks() {
for (Book book : books) {
System.out.println(book.getTitle() + " - " + book.getAuthor());
}
}
}
Usage in main
public class Main {
public static void main(String[] args) {
Library library = new Library();
Book book1 = new Book("Java for Dummies", "John Doe");
Book book2 = new Book("OOP Made Simple", "Ivan Ivanov");
library.addBook(book1);
library.addBook(book2);
library.printAllBooks();
}
}
5. Illustrations and diagrams
Diagram: what goes where?
+-----------------------------+
| Book.java |
|-----------------------------|
| public class Book { |
| private String title; | <--- fields (inside the class only)
| ... |
| public Book(...) {...} | <--- constructor (inside the class)
| public String getTitle()| <--- methods (inside the class)
| } |
+-----------------------------+
+-----------------------------+
| Main.java |
|-----------------------------|
| public class Main { |
| public static void main |
| // Creating objects |
| Book book = new Book|
| } |
+-----------------------------+
6. Review of common errors
Error 1: Class and file name mismatch
// File: Libraryuser.java
public class LibraryUser { // Class name has a capital U, file is lowercase!
// ...
}
class LibraryUser is public, should be declared in a file named LibraryUser.java
Error 2: Attempt to use an uninitialized object
Book book;
System.out.println(book.getTitle()); // NullPointerException!
Error 3: Missing braces
public class Book
private String title; // Fields are usually private for encapsulation
// ...
}
';' expected
Error 4: Declaring a method inside a method
public class User {
public void printName() {
void sayHello() {
System.out.println("Hello!"); // You cannot declare a method inside a method!
}
}
}
illegal start of type
GO TO FULL VERSION