Skip to content

Chapter 2 of 37

Basic Syntax and Structure

Build classes, main methods, statements, blocks, packages, and console programs.

26 minutes 10 quick checksBy Subha Prasad
Lesson 2 of 37Course navigation

Lesson content

Read, practise, then check your understanding

Java is case-sensitive. Source is organized into packages and types; executable statements live inside methods, constructors, or initialization blocks. A public top-level class normally matches its filename. Braces form scopes, statements usually end with semicolons, and //, /* */, and /** */ introduce line, block, and Javadoc comments.

A structured program

package learning.basics;

public final class Greeter {
    private Greeter() {}

    static String greeting(String name) {
        return "Hello, " + name;
    }

    public static void main(String[] args) {
        System.out.println(greeting("Mira"));
    }
}

main is the conventional entry point. public makes it callable by the launcher, static means no receiver object is required, and void means no result. Identifiers may contain Unicode letters but clear conventional names are preferable. Use UpperCamelCase for types, lowerCamelCase for variables/methods, and UPPER_SNAKE_CASE for constants. Keep one responsibility per type and compile frequently with warnings enabled.

Knowledge check

Answer every question correctly to complete this chapter.

Which statement best describes class?
Which Java term matches this description: A declaration that defines a reference type and its members.
Which statement best describes main method?
Which Java term matches this description: The conventional entry point with a String array parameter.
Which statement best describes statement?
Which Java term matches this description: A complete executable unit that commonly ends with a semicolon.
Which statement best describes block?
Which Java term matches this description: A brace-delimited scope containing declarations and statements.
Which statement best describes package declaration?
Which Java term matches this description: The first non-comment declaration assigning a type to a named package.

0 of 10 checks passed

Your progress is saved on this device.