O'Reilly logo

Java Garage by Eben Hewitt

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Class Definition

You can have multiple class files in the same source (.java) file, but only one of them may be declared public.

public class MyClass {
    // class definition here
}

class AnotherClass {}

Class Modifiers

You can add class modifiers to the declaration that changes the definition of the class.

public

This class modifier indicates that you want it to be available to classes other than just those in the package in which it is declared. That is, you'll let anybody use it. If your class does not include the public modifier, it can only be used by other classes within the same package.

final

To make your class so it cannot ever be subclassed, you include the final modifier in the class declaration. This example is from the standard API. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required