Java Reference
In-Depth Information
All of the Java code following the class header is called the body of the class and is enclosed in braces, { } .
Therefore, a valid class definition would be:
public class Employee {}
However, as there are no Java statements between the braces, this class does not do anything.
Java Statements
The class body is comprised of what we will refer to as class variable definitions and methods. Variables are
categorized by their scope. This text will define class variables as those variables that can be accessed by any
statement within the class. The official Java definition of class variables is much more specific and has many other
variable classifications (such as instance variables). However, the distinctions between the various variable scopes
are beyond this introductory discussion of Java.
Class variable definitions traditionally follow the class header. Simply stated, the purpose of a variable is to hold stuff,
but we will discuss this in more depth later. Variables are also be classified by the type of “stuff” they hold. For example,
the following Java statement declares (defines) a String variable called empName. String variables hold String objects:
String empName = new String();
Let's explain the various pieces of this statement. The definition begins with the variable type ( String ), at least one
space, and the name of the variable (empName). This creates a String variable called empName. Think of this as telling
the computer to reserve some space in memory and calling this space empName. Next, the equal sign assigns a value (or
in Java-speak, an object) to this variable. Please note that the Java equal sign is not like a mathematical equal sign. The Java
equal sign associates an object or value on its right to a variable on its left. So in this case, new String() creates an empty
String object (meaning no text has been assigned), and the equal sign assigns the String object to the variable empName.
Another way to look at the statement is that everything to the right of the equal sign creates the String object
and everything to the left creates the String variable empName.
Finally, there is a semicolon (;). Java statements end with a semicolon. This is a requirement. In fact, forgetting the
semicolon will be your most common coding mistake. (Try not to get too frustrated the first thousand times you do it.)
If you do not specify a semicolon at the end of a line, Java assumes that the statement is not finished and will look to the
next line for more of the statement. For example, the empName definition above can also be written as follows:
String
empName
=
new
String();
Although perfectly valid, this is one ugly looking statement.
As mentioned, strings can be assigned text. When a String object is created, a text value can be assigned
at the same time as follows:
String empName = new String("Joe Employee");
The value of a String variable can be changed very easily (which is unique for Java variables. In other words,
since most other variable can't be changed as easily, don't get used to this!) The following would assign empName
to a String object with text of Mary Worker:
empName = "Mary Worker";
 
Search WWH ::




Custom Search