Basic Structure & Variables
public class Main { public static void main(String[] args) { System.out.println("OK"); } }Main entry point for Java applications. Note that the file name must strictly match the public class name.int balance = 5000; double price = 19.99; boolean active = true;Common primitive type declarations. Pitfall: integer division like 5/2 returns 2 instead of 2.5.final double TAX_RATE = 0.18;Declares a constant whose value cannot be reassigned. Omitting final leaves the variable mutable by mistake.String city = "Dakar";Immutable string of characters. Always use .equals() instead of == to compare string content accurately.
Control Flow Statements
if (balance >= amount) { pay(); } else { decline(); }Standard conditional statement. Ensure blocks are correctly structured to avoid scope issues.String country = switch (code) { case 221 -> "Senegal"; default -> "Unknown"; };Modern switch expression (Java 14+). Do not forget the trailing semicolon at the end of assignment.for (int i = 0; i < 5; i++) { System.out.println(i); }Standard loop with counter. Beware of off-by-one errors when working with array indices.for (String name : clients) { System.out.println(name); }Enhanced for loop for collections. Pitfall: modifying the collection during iteration throws an exception.
Classes & Objects
public class Account { private String id; public Account(String id) { this.id = id; } }Class definition with constructor. Keep fields private to enforce proper encapsulation.Account acc = new Account("ACC-123");Instantiates an object in memory. Using an uninitialized reference triggers a NullPointerException.public String getId() { return this.id; }Getter method for reading private fields. Essential for controlling read access to object data.public static int total = 0;Class variable shared across all instances. Watch out for concurrency issues in multithreaded code.
OOP Core Concepts
public class Momo extends Account { public Momo(String id) { super(id); } }Class inheritance using extends. The super() call to the parent constructor must be the first statement.@Override public void validate() { System.out.println("OK"); }Overrides a parent class method. The @Override annotation helps catch signature mismatches at compile time.public abstract class Service { public abstract void execute(); }Abstract class that cannot be instantiated. Serves as a template for subclasses to implement methods.public class Api implements PaymentGate { public void pay() {} }Implements an interface contract. A class can implement multiple interfaces but extend only one class.
Error Handling & Collections
try { int res = 10 / 0; } catch (ArithmeticException e) { System.out.println(e); }Exception handling block. Avoid empty catch blocks which hide critical errors without logging.List<String> cities = new ArrayList<>(); cities.add("Brazzaville");Dynamic ordered list. Prefer declaring the interface type List on the left over concrete ArrayList.Map<String, Double> rates = new HashMap<>(); rates.put("USD", 600.0);Key-value store. Fetching a non-existent key returns null without throwing an exception.Optional<String> name = Optional.ofNullable(value);Container object to avoid NullPointerException. Use orElse() to safely provide a default fallback value.
Other resources
Need help with your website?
See the services I build your custom website.
Showcase site, online shop, management tool: let’s talk about your project.