Keyword "class" is used in Java to define a new class. A class typically contains constructors, member fields and methods. For example:

public class Kitten {

  String name;

  public Kitten(String name) { = name;

  void sayHello() {
    System.out.println("Meow, my name is " + name);


Top-level class

Most often there is one class per one Java source file. However, it is also possible to put multiple classes in one source file. The classes can be put into each other or besides each other, thus creating a tree-like structure. The classes which are not inside other classes are called "top-level classes".

If a source file contains multiple top-level classes, at most one of them can be "public". The public top-level class name must be the same as the source file name (without the extension). For example a source file called "" can contain a class "Something" which can be public, and other classes which cannot be public.

// in file ""

public class Something {

class SomethingElse {  // cannot be public

Note: If you later change your mind and want to make the "SomethingElse" class public, just move it to a separate file.

Static inner class

Static inner classes are functionally equivalent to other top-level classes. They are just declared inside of another class (or interface) to emphasise that they only make sense when used with this specific class.

A good example would be "Map.Entry", an entry in a map, which does not make sense without a "Map". Except that "Map.Entry" is technically an interface, not a class. But a specific implementation would need a class to implement this behavior, for example "AbstractMap.SimpleEntry".

Here are some examples of using static classes within the Java SE API:

  • Class "Character" contains static classes "Subset", "UnicodeBlock" and "UnicodeScript".
  • Class "AbstractMap" in package "java.util" contains static classes "SimpleEntry" and "SimpleImmutableEntry".
  • Class "KeyStore" in package "" contains static classes "CallbackHandlerProtection" and "PasswordProtection".
  • Abstract classes "Arc2D", "CubicCurve2D", "Ellipse2D", "Line2D", "Path2D", "Point2D", "QuadCurve2D", "Rectangle2D", and "RoundRectangle2D" in "java.awt.geom" package each contain two implementations called "Float" and "Double". Thus for example "Line2D.Float" represents a two-dimensional line between two points with "float" coordinates.
  • Abstract classes "NumberFormat" and "DateFormat" in "java.text" package each contain a static class "Field" representing a value which may appear in a formatting string (such as: day of a week in a Date, or exponent in a Number). These values are used in their specific implementations, such as "DecimalFormat" or "SimpleDateFormat".

Inner class

Inner classes are used with the class which contains them. Each inner class object contains a reference to the outer class object. For example, imagine ants in an ant-hill, where each ant remembers where it belongs.

public class AntHill {

  public String name;

  public class Ant {
    public void describe() {
      System.out.println("An ant from " +;


Each ant belongs to an anthill, therefore to create an ant you have to provide an ant-hill instance:

AntHill ah = new AntHill(); = "ant-hill in my garden";
Ant a = Ant();

Note the specific syntax for inner classes: To create a new instance you have to provide an instance of the outer class. Within a non-static method of the outer class, you don't have to do it explicitly; "this" is implicitly assumed. To access the instance of the outer class from the inner class, write "OuterClassName.this".

Class java.lang.Class