ТОП просматриваемых книг сайта:
OCP Oracle Certified Professional Java SE 17 Developer Study Guide. Jeanne Boyarsky
Читать онлайн.Название OCP Oracle Certified Professional Java SE 17 Developer Study Guide
Год выпуска 0
isbn 9781119864592
Автор произведения Jeanne Boyarsky
Жанр Программы
Издательство John Wiley & Sons Limited
java -cp "C:\temp\directoryWithJars\*" myPackage.MyClass
This command will add to the classpath all the JARs that are in directoryWithJars
. It won't include any JARs in the classpath that are in a subdirectory of directoryWithJars
.
Creating a JAR File
Some JARs are created by others, such as those downloaded from the Internet or created by a teammate. Alternatively, you can create a JAR file yourself. To do so, you use the jar
command. The simplest commands create a jar
containing the files in the current directory. You can use the short or long form for each option.
jar -cvf myNewFile.jar . jar --create --verbose --file myNewFile.jar .
Alternatively, you can specify a directory instead of using the current directory.
jar -cvf myNewFile.jar -C dir .
There is no long form of the -C
option. Table 1.4 lists the options you need to use the jar
command to create a JAR file. In Chapter 12, you see jar
again for modules.
TABLE 1.4 Important jar
options
Option | Description |
---|---|
-c --create
|
Creates a new JAR file |
-v --verbose
|
Prints details when working with JAR files |
-f <fileName> --file <fileName>
|
JAR filename |
-C <directory>
|
Directory containing files to be used to create the JAR |
Ordering Elements in a Class
Now that you've seen the most common parts of a class, let's take a look at the correct order to type them into a file. Comments can go anywhere in the code. Beyond that, you need to memorize the rules in Table 1.5.
TABLE 1.5 Order for declaring a class
Element | Example | Required? | Where does it go? |
---|---|---|---|
Package declaration |
package abc;
|
No | First line in the file (excluding comments or blank lines) |
import statements
|
import java.util.*;
|
No | Immediately after the package (if present) |
Top-level type declaration |
public class C
|
Yes | Immediately after the import (if any) |
Field declarations |
int value;
|
No | Any top-level element within a class |
Method declarations |
void method()
|
No | Any top-level element within a class |
Let's look at a few examples to help you remember this. The first example contains one of each element:
package structure; // package must be first non-comment import java.util.*; // import must come after package public class Meerkat { // then comes the class double weight; // fields and methods can go in either order public double getWeight() { return weight; } double height; // another field - they don't need to be together }
So far, so good. This is a common pattern that you should be familiar with. How about this one?
/* header */ package structure; // class Meerkat public class Meerkat { }
Still good. We can put comments anywhere, blank lines are ignored, and imports are optional. In the next example, we have a problem:
import java.util.*; package structure; // DOES NOT COMPILE String name; // DOES NOT COMPILE public class Meerkat { } // DOES NOT COMPILE
There are two problems here. One is that the package
and import
statements are reversed. Although both are optional, package
must come before import
if present. The other issue is that a field attempts a declaration outside a class. This is not allowed. Fields and methods must be within a class.
Got all that? Think of the acronym PIC (picture): package, import, and class. Fields and methods are easier to remember because they merely have to be inside a class.
public
classes in a code snippet or question, you can assume they are in different files unless it specifically says they are in the same .java
file.
Now you know how to create and arrange a class. Later chapters show you how to create classes with more powerful operations.
Creating Objects
Our programs wouldn't be able to do anything useful if we didn't have the ability to create new objects. Remember that an object is an instance of a class. In the following sections, we look at constructors, object fields, instance initializers, and the order in which values are initialized.
Calling Constructors
To create an instance of a class, all you have to do is write new
before the class name and add parentheses after it. Here's an example:
Park p = new Park();
First you declare the type that you'll be creating (Park
) and give the variable a name (p
). This gives Java a place to store a reference to the object. Then you write new Park()
to actually create the object.
Park()
looks like a method since it is followed by parentheses. It's called a constructor, which is a special type of method that creates a new object. Now it's time to define a constructor of your own:
public class Chick { public Chick() { System.out.println("in constructor"); } }
There are two key points to note about the constructor: the name of the constructor matches the name of the class, and there's no return type. You may see a method like this on the exam:
public