Java classes

Java classes are stored in .java files. Each .java file may contain several classes. They are compiled by the Java compiler javac and stored in .class files. Each .class file contains one compiled class only.

Each .java file contains only one public class. The keyword public in front of the class name makes it accessible from the classes in other files. The filename must match the public class name. The file can contain other classes too, they are compiled into their own .class file, but they can be accessed only by the public class that gave its name to the .java file. 

This is what the content of the file MyClass.java might look like:

public class MyClass {  private int field1;  private String field2; public String method1(int ...

Get Introduction to Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.