Understanding the Structure of Java Classes - dummies

Understanding the Structure of Java Classes

By John Paul Mueller

It’s impossible to create a Java application without creating a class. However, the ways in which you’ve use classes in the beginning when you’re learning about them have been relatively simplistic — Java is capable of considerably more complexity, which is the point of this chapter.

Classes have a specific structure. Just as the blueprints used to create buildings have certain features and follow predefined formats, so do the classes you create. Otherwise, the JRE could never interpret your design and use the class to construct objects within an application.

As part of designing a class, you must also understand how to show and hide data and code elements in your class, which means that this chapter must provide some additional information about scope. Using scope correctly makes your class more secure because you can hide data from prying eyes.

A properly scoped variable also makes the class more reliable by making it less likely that someone will be able to fill the variable with incorrect data. Another good reason to employ scope is to make your class easier to use by hiding details that other developers don’t need to see in order to use the class.

Java has had anonymous inner classes (usually shortened to anonymous classes) to create ad hoc classes where a formal class description is unnecessary or overkill. For example, you might use an anonymous class to change the standard behavior of a formal class in just one instance.

Using an anonymous class makes your code cleaner, shorter, and easier to understand. It also helps optimize the code in many cases so it conceivably runs faster. Java 8 also includes a new feature called lambda expressions that make the code even shorter.

A lambda expression is a special kind of anonymous class that contains just one functional interface and one method, so you can describe what to do with a bare minimum of code. The result is that anyone viewing your code will be able to focus on what the code does rather than the structure around the code.

A building contractor can understand a blueprint designed by an architect because both of them use the same set of rules and conventions. When you create a class, you’re the architect. The blueprint you create must communicate your ideas to the computer, which plays the role of the builder.

For this communication to work, you must both use the same rules and conventions. The following sections describe the rules and conventions for creating classes in Java by dividing the task into functional areas, such as writing a method.

A convention is an agreement or a contract between you and the JRE. In fact, when you read other documentation, it may actually use the term contract to express the idea of a convention. No matter what term you use, the convention expresses the responsibilities of each party in defining the class and building it as an object.

In some respects, you can also view a convention as a custom — the set of practices that Java developers have created for developing classes. There isn’t a right or wrong about the conventions that Java uses; this set of practices is simply what Java developers have created over time as the most efficient way of defining a class.