new Cat("Ziggy")
would call the constructor for the Cat
class. Doing this instantiates a new Cat
object.Cat
class may extend a Pet
class. It may have some same functionality, e.g. weight, number of legs, age fields and accessors, but it may also have its own methods, e.g. purr
.this
can be used in a method to refer to the object itself, and to provide access to the object’s data fields. If there is no ambiguity, you can also refer to the data fields directly.The scope of a class, method, or variable refers to the part of the program where the class/method/variable is visible and can be accessed. In Java, the scope of a class and the scope of each class attribute (method or data field) is specified using the following access modifiers:
The scope is the first thing specified when a class or class attribute is being defined. In the example below, objects of type Foo
can be created anywhere in the program. The Foo
class methods are also public, which means these methods can be called on Foo
objects anywhere in the program. However, The variable xBar
is private, so the only place this variable can be accessed is inside of class Foo
.
package fragle;
public class Foo {
private int xBar = 42;
public int getXBar() {
return xBar;
}
public void addToXBar(int y) {
xBar += y;
}
}
Variables are identifiers in a program that hold values. Java employs several different kinds of variables, which vary in terms of their scope.
this
keyword; for example, this.x = x
where the x
on the left is the instance variable and the x
on the right is a parameter or local variable.
Zip
has a static variable called numObjects
, throughout the class the variable would be referenced as Zip.numObjects
.
int
or char
, or it can be a class or interface.A quick summary:
Variable | Scope | Lifetime | Type Declaration | Initialization |
---|---|---|---|---|
Instance | each object | as long as object is around | top of class file | constructor |
Static | any object of the class | always there | top of class file | declaration |
Local | innermost {…} | only within the {..} | method body | declaration |
Parameter | method | duration of method call | method signature | method call |
Imagine a Student
class. Which type of variable is best to use for each of the following pieces of information?
isOlderThan
, which compares a student’s current age to a provided target age, the variable that holds the provided age with which to compare the student’s current age.isOlderThan
, the boolean variable that holds the result of comparing the two ages.Java files use the extension .java
. They consist of the following:
Import statements are used to avoid having to refer to classes by their fully qualified names.
Classes can be used without being imported by fully qualifying the class name. For example, graphics.Rectangle
is how the Rectangle
class would need to be referred to if the class was not imported.
Using import statements in your file allows you to use shorter forms to reference classes. Below are three possible import statements that could be used to import the Rectangle
class.
import graphics.Rectangle;
import graphics.*;
import graphics.Rectangle.*;
Rectangle
class directly in our code, e.g., new Rectangle
.graphics
package.Rectangle
class available directly.As an example, if Rectangle
had a static draw
method, then with the first two imports we can refer to this method as Rectangle.draw(...)
, while with the last import we can do draw(...)
instead.
Class definitions have the keyword class
, possibly preceded by an access modifier and followed by the class name. In Java, it is conventional to capitalize class names. Top-level classes must be contained in a file that has the same name as the class.
The class name may be followed by extends ...
and/or implements ...
clauses, if the class is a subclass of another class or if it implements a certain interface. For example,
public class Circle extends AbstractShape implements Drawable {
...
}
The interior of a class definition may contain any of the following, in any order:
Interface definitions are similar to class definitions with the following exceptions:
extends
part, unless it is to extend another interface.implements
part.Method declarations have no body; instead, a method declaration ends with a semicolon.
Abstract class definitions are similar to regular class definitions with the following exceptions.
abstract
modifier in their definitions.Methods that are simply declared must contain the abstract
keyword modifier.
A field declaration specifies the visibility of the field, its data type, and possibly an initial value. Below are some examples:
private String firstName;
firstName
that is of type String
. This field will likely be initialized in the constructor.static final int MAX_CAPACITY = 40;
MAX_CAPACITY
is visible within the package (hence no access modifier in front). It is of type int
and has been declared final, meaning its value cannot be changed.public static String hostname;
hostname
that is visible everywhere. Using the static
keyword modifier indicates that hostname
is a class variable, with one copy of the variable being shared by all class objects.Method definitions begin with modifiers followed by the method’s return type, the method name, and a parenthesized list of parameter declarations. The body of the method then follows, enclosed in curly braces. Below are some examples:
public void setFirstName(String newFirstName) {
...
}
public String getFirstName() {
...
}
private static boolean isValidName(String firstName, String lastName) {
...
}
Note that if your program has a main method, this method has a specific required signature. For example:
public static void main(String[] args) {
System.out.println("Hello world!");
}
Constructors are similar to method definitions. The name of a class also functions as the name for the class constructor. Contructors cannot be static. They also do not explicitly return a value from within their body. Instead, the newly created object is automatically returned.
A class can have multiple constructors as long as the constructors have differences in their parameter lists. When a class has multiple constructors, it is customary for all constructors to eventually call the same constructor, the one with the most complete set of arguments.
A class implemented without a constructor will be given the following default constructor: public void ClassName() {}
.
Example:
public Person(String firstName, String lastName, int age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
// This constructor simply calls the longer constructor.
public Person(String firstName, String lastName) {
this(firstName, lastName, 0);
}
Constructors are called when the keyword new
is used to create a class instance. For example, Person p = new Person("Peter", "Doe", 26);
will automatically call the Person
class constructor.
It also common practice to create static methods for classes that might have multiple constructors. These methods then provide a name for how you are constructing the object.
Java contains many of the standard control elements you may have seen in other languages to control the flow of execution:
Java conditionals follow a standard if-then-else
pattern:
if (thisIsTrue) {
... true case code
}
if (thisIsTrue) {
... true case code
} else {
... false case code
}
if (thisIsTrue) {
... true case code
} else if (thatIsTrue) {
... case where thatIsTrue is true but thisIsTrue is false
} else {
... both false
}
There is also the ternary operator, which returns a value. For example,
int x = (y > 5) ? 5 : y;
This can be translated as “if y is greater than 5 then return 5; otherwise, return the value of y”.
Java implements the familiar while loop that executes a block of code as long as a specific condition is true. For example,
while (account.hasMoney()) {
account.withdraw();
}
This loop will continue executing the account.withdraw()
method as long as account.hasMoney()
is true.
Java provides two kinds of for loops. The standard counter loop and a foreach loop for iterating over a collection.
for (int i = 0; i < allNames.length; i++) {
System.out.println(allNames[i]);
}
for (String name : names) {
System.out.println(name);
}
The second for loop will iterate over all the items in names
using the variable name
as its loop variable. This kind of for loop is preferred if you simply need to loop over the elements of a collection and you have no need for the loop index i
. And as an extra benefit, it works for all sorts of iterables and not just for arrays. Any object whose class implements the Iterable interface can be used in this so-called for-each loop.
A switch statement compares a value against a series of constant values presented in case
expressions, and executes the corresponding statements. It will fall through to the next case unless you remember to use break
.
switch (person.getType()) {
case "faculty":
doSomething();
break;
case "student":
doSomethingElse();
break;
default:
// We don't do anything otherwise
}