



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
with the basic techniques and definitions of Java covered in that course. ... Java programs and object oriented programming in general.
Typology: Study notes
1 / 5
This page cannot be seen from the preview
Don't miss anything!




Almost all of you have taken COMP 202 or equivalent, so I am assuming that you are familiar with the basic techniques and definitions of Java covered in that course. Those of you who have not taken a COMP 202 or equivalent should spend some time in the first week or two familiariz- ing yourself with the differences between Java and the programming language that you did learn (probably C and/or Fortran). I would strongly advise you either to buy/borrow an “Intro to Java” book and read as much of it as you can. There are dozens in the Schulich library, in particular, the two mentioned in the course outline are very good. There are also free Java books on the web. The course web page has a link to one (which I have not read, but which some students tell me is good). Here I am briefly reviewing some of the basic vocabulary that we will use when talking about Java programs and object oriented programming in general. The purpose of this review is mainly to refresh your memory, in particular, to highlight those Java-specific terms and concepts that will be important in the weeks ahead. I will concentrate my review on object oriented terms, which those of you who know only C or Fortran will not be familiar with and will find rather strange at first. You need to become familiar with many of these terms in the next two weeks, in particular, when you start the programming assignments. So get cracking! There are various versions of COMP 202 so there may be a few terms below that weren’t covered in the course you took. No worries. You can catch up on these things when the time comes.
Classes and objects
A class consists of a header and a body. The header consists of modifiers and an identifier i.e. name. The body consists of fields and methods. Some classes contain constructor methods. Other classes contain a main() method. (It is possible for a class to have both constructors and a main method, though this is atypical.)
fields
methods
classname
Here are some terms we use and rough definitions
One often says that an object ’belongs’ to a class, and so you might be tempted to think of a class as a set of objects (in the mathematical sense). This is not quite correct, however. A class exists without any program running, whereas objects exist only when a program is running.
Variables
which were public. This distinction of private and public allows you to restrict access to within a class or between classes. There are other levels of access, though, and so I would like to mention these, just to familiarize you. (You will see these when you use Eclipse and you may wonder what they are.)
In order to define these other levels of access, we need to know what a package is. A package is a set of classes. You should go to the Java API
http://java.sun.com/j2se/1.5.0/docs/api/.
On the upper left corner of that web page is a listing of dozens of packages. A few of them that you are familiar with are:
The full name of a class is the name of the package following by the name of the class, for example java.lang.Object. You can have packages within packages e.g. java.lang is a package within the java package. The package and class structures correspond exactly to how the class files are organized in the file system. e.g. you have directors (or folders) and subdirectories (or subfolders) and finally the class files themselves. To identify a class as being part of a package, you start the class with the package name. The first line of your class file will be package PackageName If you want to use a package written by someone else – e.g. if you are writing a class that extends a class from that package – you start your class with a statement import packagename This doesn’t actually copy the code into your file. Rather, it just says that you will be using files from that package so when a class is referenced (e.g. instantiated), the Java compiler knows where to look.
Let’s next consider modifiers for the variables and methods in a class. In order of decreasing visibility, they are:
There is also a modifier called protected but you need to know about class inheritance to under- stand what it is. (We will cover inheritance later in the course.)
final
The final modifier can mean several different things. For now, you will only use it to define constants i.e. variables whose values are defined once and cannot be changed. e.g.
final double PI = 3.
Note that you need to assign a value for this to make sense. To understand other uses, you need to know about inheritance.
static
The modifier static specifies that a variable or method is associated with the class, not with particular instances (objects). It is used, for example, to keep track of the number of instances of a class, or the number of times that a method is invoked.
static int numberOfObjects; // A constructor would increment static int getNumberOfObjects(){ // this variable. return numberOfObjects; }
Another example of a static method is main(). You should think of a static method or field as being in the class definition, not in particular objects. Thus, a static method cannot contain statements that refer to instance fields or that invoke instance methods. The reason is that these instance fields and methods belong to particular instances of the class. Note that you can have an instance method that invokes a static method or references a static field in a class. You can also have a static method that references an instance variable. You do this all the time when you use methods such as sin() or exp in the java.lang.Math class. Such methods are static (and final). Note that to use such methods, in your class definition you would specify that you are using the package java.lang and then you would invoke the method just by stating the classes name and the method e.g. Math.cos( 0.5 ). Note that the class name is used instead of a reference variable (to an object) which is why we say that static methods are “class methods” rather than “instance methods”.