Java Language: Reserved Words, Object Structure and Their Definitions
Although you can create first-class objects in Java language, not everything is an object. Two things make Java language different from purely object-oriented languages: Java language is a mixture of objects and primitive types, and Java language allows the programmer to write code that exposes the inner workings of one object to any other object that uses it. Reserved Words Like any programming language, Java language designates certain words that the compiler recognizes as special, where the following words cannot be used to name constructs:
,
, and
are literals and are also not to be used as Java construct names. An advantage of programming with I.D.E. is the use of syntax colouring for reserved words. Structure of A Java Object Since an object is a discrete entity that contains attributes and behavior, it has a crisp boundary, a state, and can do things when asked correctly. Every object-oriented program has rules on how to define an object. In Java language, an object can be defined by the following, called Listing 1:
This Listing 1 contains many constructs. The constructs highlighted are literals, which are found in the reserved words, and therefore, in any object definition, must be what they are written like in Listing 1. The other, non-highlighted constructs were named to represent their concepts, where the concepts with the square brackets are not required in code, such as "= initial value" and "argumentList". Square brackets are also not part of the Java syntax. Comments in Code (refer to Listing 1, lines 11-15) As seen in Listing 1, almost every programming language allows the programmer to add comments to help document the code. Java syntax allows single-line and multi-line comments, however the single-line comments must always be on one line, and, although multi-line comments can span any numbers of lines, it must begin and terminate with /* and */, respectively. Packaging Objects (refer to Listing 1, line 1) Even though Java language allows the programmer to choose the names for their objects, there may be times where the programmer ends up using the same name to express two different concepts, causing something called name collision. Java language resolves name collisions with packages, or Java packages. A Java package is a mechanism for providing a namespace, which is an encapsulated area where names are unique, however shown publicly as the same. To define a package in code, the programmer uses the keyword
followed by a legal package name, and then terminated with a semi-colon. A programmer is not forced to specify a package if all of their objects have unique class names and reside in the default package.
Often package names, such as above, are separated by periods.
is the organization type, such as com, org, net, etc.
is the name of the organization's domain, such as sun, ibm, etc.
is the name of the application, abbreviated.
is the name of the component. Import Statements (refer to Listing 1, line 2) An import statement allows the Java compiler to know where your classes are coming from; what location to find them in.
The programmer specifies what to import using the keyword,
, followed by the class including its package, and then terminated with a semi-colon. If the programmer wishes to import all classes within a package, they would put
after the package name.
Class Declaration (refer to Listing 1, lines 3-10) In order for Java to know an object, the programmer must declare a class. A class is like the template, or the default shape, for an object. A class defines an object's basic structure, and at run time, the application creates an instance of the object. A instance is the specific realization of the object, and so while, say, there are types of beverages, its instance would be a specific beverage. A class defines the structure of a certain thing, while the object is an instance of that thing. With
, there are several values that can be put here, but its general value is
. Example of a class declaration:













