AnatomyOf

Anatomy of a Java file

example.java
1package com.example.demo;
2
3import java.util.ArrayList;
4
5public class Counter {
6 private int total; // one int, guarded by an entire class. Java.
7
8 public Counter() {
9 this.total = 0;
10 }
11
12 public void addAll(ArrayList<Integer> values) {
13 for (int value : values) {
14 total += value;
15 }
16 }
17
18 public static void main(String[] args) {
19 // AbstractFactoryFactory not required for this one
20 Counter counter = new Counter();
21 ArrayList<Integer> values = new ArrayList<>();
22 values.add(5);
23 counter.addAll(values);
24 System.out.println(counter.total); // 5 lines to add one number
25 }
26}
Java is a statically-typed, object-oriented programming language designed to have as few implementation dependencies as possible ("write once, run anywhere").