1.8. Creating a Java Class
Problem
You want to create a new Java class in an existing Java project.
Solution
When you have the Java perspective open and have a Java project selected in the Package Explorer, you can create new classes in Eclipse in several ways. You can use the toolbar item with the circled C icon, you can select File→ New→ Class, or you can right-click a project in the Package Explorer and select New→ Class in the context menu. All these methods open the New Java Class dialog.
Discussion
The New Java Class dialog is shown in Figure 1-9.

Figure 1-9. Creating a new Java class
Over the previous few recipes, we’ve been developing
a short Java project, FirstApp. Now
we’re going to use the following code to display a
message in that project:
public class FirstApp
{
public static void main(String[] args)
{
System.out.println("Stay cool.");
}
}Creating Java classes is a fundamental skill for most Eclipse
developers, so I’m going to cover the basics here.
Note the options in this dialog: you can set a
class’s access specifier as
public, private, or
protected; you
can
make the class abstract or
final; you can specify the
new
class’s superclass
(java.lang.Object is the default); and you can
specify which, if any, interfaces it implements. Class creation is
covered in more detail in Chapter 3.
In this book, you’ll put examples into Java packages to avoid any conflict with ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access