Understanding the Class Structure
In Java programs, classes are the basic building blocks. When defining a class, you describe all the parts and characteristics of one of those building blocks. In later chapters, you see other building blocks such as interfaces, records, and enums. To use most classes, you have to create objects. An object is a runtime instance of a class in memory. An object is often referred to as an instance since it represents a single representation of the class. All the various objects of all the different classes represent the state of your program. A reference is a variable that points to an object. In the following sections, we look at fields, methods, and comments. We also explore the relationship between classes and files.
Fields and Methods
Java classes have two primary elements: methods, often called functions or procedures in other languages, and fields, known as variables. Together these are called the members of the class. Variables hold the state of the program, and methods operate on that state. If the change is important to remember, a variable stores that change. That’s all classes really do. It’s the programmer’s job to create and arrange these elements in such a way that- the resulting code is useful and, ideally, easy for other programmers to understand.
The simplest Java class you can write looks like this:
1: public class Human {
2: }
Java calls a word with special meaning a keyword, which we’ve marked bold in the previous snippet. Throughout this thread, we often bold parts of code snippets to call attention to them. Line 1 includes the public keyword, which allows other classes to use it. The class keyword indicates you are defining a class. Human
gives the name of the class. With that, this isn’t an interesting class, so let’s add your first field.
1: public class Human {
2: String name;
3: }
The line are just there to make the code easier to discuss, they are not part of the program;
On line 2, we define a variable named name
. We also declare the type of that variable to be String
. A String
is a value that we can put text into, such as “I am in quotes”. String
is also a class supplied with Java. Next we can add methods.
1: public class Human {
2: String name;
3: public String getName() {
4: return name;
5: }
6: public void setName(String humanName) {
7: name = humanName;
8: }
9: }
On lines 3–5, we define a method. A method is an operation that can be called. Again, public
is used to signify that this method may be called from other classes. Next comes the return
type—in this case, the method returns a String
. On lines 6–8 is another method.
This one has a special return type called void
. The void
keyword means that no value at all is returned. This method requires that information be supplied to it from the calling method; this information is called a parameter
. The setName
() method has one parameter named humanName
, and it is of type String
. This means the caller should pass in one String parameter and expect nothing to be returned. The method name and parameter types are called the method signature. In this example, can you identify the method name and parameters?
public int numberGuests(int month) {
return 10;
}
The method name is numberGuests
. There’s one parameter named month
, which is of type int, which is a numeric type. Therefore, the method signature is numberGuests(int)
Comments in java
Another common part of the code is called a comment. Because comments are not executable
code, you can place them in many places. Comments can make your code easier to read.
There are three types of comments in Java. The first is called a single-line comment:
// single line comment
A single-line comment begins with two slashes. The compiler ignores anything you type after that on the same line. Next comes the multiple-line comment:
/* Multiple
* line comments
* comes here
*/
A multiple-line comment (also known as a multiline comment) includes anything starting from the symbol /* until the symbol */. People often type an asterisk (*) at the beginning of each line of a multiline comment to make it easier to read, but you don’t have to. Finally, we have a Javadoc comment:
/**
* Javadoc multiple-line comment
* @author Jeanne and Scott
*/
This comment is similar to a multi-line comment, except it starts with /**. This special syntax tells the Javadoc tool to pay attention to the comment. Javadoc comments have a specific structure that the Javadoc tool knows how to read. You probably won’t see a Javadoc comment on the exam. Just remember it exists so you can read up on it online when you start writing programs for others to use. As a bit of practice, can you identify which type of comment each of the following six words is in? Is it a single-line or a multi-line comment?
/*
* // something
*/
// bishop
// // engineer
// /* lawyer */
/* doctor */
/*
* /* nurse */
*/
Classes and Source Files
Most of the time, each Java class is defined in its own .java
file. Here, the only top-level type is a class. A top-level type is a data structure that can be defined independently within a source file.
A top-level class is often public, which means any code can call it. Java does not require that the type be public. For example, this class is just fine
1: class Human {
2: String address;
3: }
You can even put two types in the same file. When you do so, at most one of the top-level types in the file is allowed to be public. A good example is as follows:
1: public class Human {
2: private String name;
3: }
4: class AnotherHuman {}
If the file contains a public
type, it needs to match the filename. The declaration public
class AnotherHuman
would not compile in a file named Human.java.