Anatomy of a Java file
Java is a statically-typed, object-oriented programming language designed to have as few implementation dependencies as possible ("write once, run anywhere").
File extensions: .java
Every part of the example below is labelled and explained. This page is one of 55 annotated tours on AnatomyOf, a free, open-source project by LunarWerx Studios.
What is inside a Java file
Package declaration
Defines the namespace for the class.
A package statement, when present, must be the first non-comment declaration in the file. It groups related classes under a dotted namespace such as com.example.anatomy. Source trees conventionally mirror that name as com/example/anatomy/JavaAnatomy.java, and build tools generally expect the layout, though javac itself can compile an explicitly named source file from another location. Classes in the same package can reference each other without an import. Omitting the package statement entirely places the class in the unnamed "default package," which works for small experiments but is avoided
Import statements
Brings in classes from other packages.
import java.util.List; lets the rest of the file refer to List instead of writing the fully qualified java.util.List every time. A wildcard form like import java.util.*; imports every public type in that package, though most style guides discourage it because it obscures where a name comes from and can create ambiguity if two packages export a type with the same name. Everything in java.lang (String, Object, System, and friends) is imported automatically and never needs its own import line. Classes in the same package are likewise visible without an import.
Javadoc comment
Documentation for classes, methods, and fields (/** ... */).
A comment opened with /** (two asterisks, not one) is a Javadoc comment. Placed directly above a class, method, or field with no blank line in between, it becomes that member's official documentation, extracted by the javadoc tool into browsable HTML pages like the ones at docs.oracle.com. Tags such as @param, @return, and @throws document a method's parameters, return value, and exceptions in a structured way that IDEs also read to power autocomplete tooltips. An ordinary /* ... */ block comment (single leading asterisk) compiles identically but is invisible to javadoc.
Class definition
The blueprint for objects; must match the filename (public class ...).
Java is thoroughly object-oriented: essentially all code, including main, must live inside a class. A public top-level class named JavaAnatomy must be declared in a file named exactly JavaAnatomy.java. The compiler enforces this one-public-class-per-file rule, unlike languages that let any file define any number of top-level constructs. A class defines the fields and methods that describe a kind of object; new creates individual instances from that blueprint. A file may also contain additional non-public (package-private) classes alongside its single public one.
Field (class variable)
Stores data for the class or its objects.
A field declared directly in the class body, like private String name;, holds state that belongs to each object created from the class. Every instance gets its own copy unless the field is also static. Access modifiers (private, public, protected, or none/package-private) control which other code can read or write the field directly. Adding static makes a field belong to the class itself rather than any one instance, so all objects share a single copy, commonly paired with final for a compile-time constant such as private static final int MAX_COUNT = 100;. Java is statically typed, so every fi
Constructor
Special method called when an object is instantiated.
A constructor shares its name with the class and has no return type, not even void. public JavaAnatomy(String name) { this.name = name; } runs once, at the moment new JavaAnatomy(...) creates an object, and is typically used to assign initial values to fields. this inside a constructor refers to the object currently being built, and disambiguates a parameter from a field of the same name, as in this.name = name;. If a class defines no constructor at all, the compiler silently supplies a no-argument default constructor; that default disappears as soon as any constructor is written explicitly.
Method definition
A reusable block of code that performs an action.
A method declares an access modifier, a return type, a name, and a parenthesized parameter list, e.g. public void printGreeting(). void means the method returns nothing; any other return type obliges every code path through the method to return a value of that type. Methods are invoked on an instance (demo.printGreeting()) unless declared static, in which case they belong to the class itself and are called through the class name. Java supports overloading (multiple methods with the same name but different parameter lists) resolved at compile time by the argument types used at the call site.
Main method
The entry point for a Java application (public static void main(String[] args)).
The JVM starts a program by looking for exactly this signature: public static void main(String[] args). Every keyword is load-bearing. public so the JVM launcher can reach it from outside the class, static so it can be invoked without first constructing an instance, void because it returns nothing to the operating system beyond its exit code, and String[] args to receive command-line arguments. Before any of this runs, the source file is compiled by javac into platform-independent bytecode (a .class file), which the JVM then interprets or JIT-compiles at runtime: the basis of Java's "write onc
Variable declaration (local)
Declares variables within a method or block.
A local variable such as List<Integer> numbers = new ArrayList<>(); must state its type (List<Integer>) because Java resolves types at compile time rather than inferring them dynamically at run time. The <Integer> part is a generic type parameter that tells the compiler this list may only ever hold Integer values, catching type mismatches before the program runs. Since Java 10, the var keyword lets the compiler infer an obvious local variable's type from its initializer (var numbers = new ArrayList<Integer>();), saving typing without giving up static typing. The inferred type is still fixed an
Object instantiation
Creates a new instance of a class with new.
new JavaAnatomy("Java Learner") allocates memory for a fresh object on the heap, then runs the matching constructor to initialize it, and finally evaluates to a reference to that new object. new is the normal explicit construction syntax for classes and arrays, while factories, literals, reflection, deserialization, and boxing can also produce object references without showing new at the call site. Because objects live on the heap and are accessed through references rather than copied by value, assigning one variable to another (JavaAnatomy a = demo;) copies the reference, not the object, both
Control flow (loop)
Executes code repeatedly (e.g., for, while).
The classic three-clause for (int i = 0; i < 3; i++) { ... } declares a loop variable, a continuation condition checked before each pass, and an update executed after each pass. Java also has while (condition checked up front) and do...while (condition checked after the first pass, guaranteeing at least one iteration). The enhanced for-loop, for (Integer n : numbers) { ... }, iterates directly over any array or Iterable without manual indexing. break exits the nearest loop immediately, and continue skips straight to the next iteration's check.
Method call
Executing a method on an object or class.
demo.printGreeting(); invokes the printGreeting method on the object referenced by demo, and numbers.add(i); calls add on the ArrayList instance numbers. The compiler resolves which method to run by matching the receiver's type and the argument types against the available overloads. System.out.println(...) is likewise a method call: System.out is a static field of type PrintStream on the System class, and println is invoked on that object. Calling a static method looks similar but goes through the class name instead of an instance, e.g. Math.max(a, b).
Official Java site · All languages on AnatomyOf
The interactive tour needs JavaScript. Enable it to hover a callout and trace it into the code.