Tools
Log in
English

Learn to code

Learn Java

Based on Java 17

Java classes, encapsulation, inheritance and project layout, with examples.

1Classes and objectsA class is the blueprint, an object is a real thing built from it. Covers constructors and this.Example: reading log2EncapsulationHide values that must stay valid, like a balance, and change them only through chosen methods.Example: bank account3InheritanceInherit fields and methods from a parent class and override only what differs.Example: dogs and cats4PolymorphismOne area() call works out a circle, a rectangle and a triangle each in its own way.Example: total area of shapes5AbstractionUse an interface to name the job and an abstract class to share the common steps.Example: sending notices6Composition and staticPut "has a" relations in fields (composition) and keep shared values in static members.Example: car and engine7Access modifiersSee in one table how far public, protected, no modifier and private reach.Example: safe+Project structurePick a command-line app, a web API or a library and get the Maven or Gradle folder tree with starter files as a zip.zip download

Suggested order

Read from classes and objects onward. Each example is the starting point for the next topic. By encapsulation you can build one solid object; inheritance, polymorphism and abstraction show how classes work together.

Composition and static, and then access modifiers, answer questions left open earlier ("when should I avoid inheritance?", "how far does protected reach?"). Finish with project structure and build a real project folder.

Where the output comes from

Every example was compiled with javac and run with java on OpenJDK 17, and the output is pasted as it came out. The variable values in the step-through were checked against the Java debugger (jdb) stopped on the same lines.

Java does not run on this page. Run your edited code on your own computer like this.

javac Main.javajava Main # Since Java 11 this also works in one linejava Main.java

Java version

Text and examples follow Java 17 (LTS) and run unchanged on newer versions. Features added in versions before 17 are marked at the top of the page, for example "Java 16+".

Where Java came from

Java began in 1991 at Sun Microsystems in the United States, in a team led by James Gosling. Its first name was Oak and it was demonstrated on a handheld device, Star7, in 1992; the language went public under the name Java in 1995, and the first JDK, 1.0, shipped in January 1996. Oracle bought Sun in 2010, and since then Oracle and the OpenJDK community have carried the releases. Since Java 10 in 2018 a new version has come out every six months, and 27 arrived in September 2026.

Large company back ends such as banks and online shops, Android apps, and big-data tools like Hadoop and Kafka run on Java or the JVM. Bytecode compiled once runs on any system that has a JVM, and strict type checks help on projects that many people change over years. For a ten-line automation script or a quick throwaway tool, Python usually takes less typing.

On this site, read from classes and objects onward, then grab a Maven or Gradle skeleton in project structure and run it on your own machine. Compare languages shows how the same example looks elsewhere.

Tools that pair well Compare languages Java project structure JSON formatter

Made bySun Microsystems (USA), a team led by James Gosling
First releaseAnnounced 1995 · JDK 1.0 in January 1996
Run today byOracle (bought Sun in 2010) and the OpenJDK community
Used mostly forBusiness back ends, Android apps, big-data processing

Questions people ask

I am new to Java. Where should I start?

Start with classes and objects and go in order. The pages assume you already know basics such as variables, if and for.

Which Java version do the examples need?

Java 17 or newer runs every example as written. Features from earlier versions are marked at the top of each page.

How do I run an example on my computer?

Save it as Main.java, run javac Main.java and then java Main. Since Java 11, java Main.java also works in one step.