Last modified: January 07, 2024
A typical Java class has the following structure:
public class MyClass {
// Class Variables - static fields storing info about the
// state of the class
// Instance Variables - fields storing info about the state
// of an object (attributes/properties)
// Constructors - Specialized methods that set an object up
// and initialize instance variables
// Methods - The things an object can do
// functions that perform actions and/or return a value
}
A few things to notice:
Class names are capitalized and use UpperCamelCase.
Class names match the filename EXACTLY.
For example, public class Fish would be in a file named Fish.java
The class begins with an opening brace "{" and ends with a closing brace "}"
//
everything after the //
is considered a comment. Comments are ignored when compiling the code./*
and */
is also considered a comment. This can be used to comment out multiple lines or just part of a line.