To make a variable we make the label in memory, then attach something to it.
We can make an object variable using a particular class:
ClassName objectName = new ClassName();
We can access code inside an object using the dot operator:
objectName.variable = 10;
double x = objectName.variable;
But how do we run code in other classes?
Running code
We already have!
System.out.println("Hello World");
This is running the code called "println()".
Not only does this run code, but it seems to send the code some data ("Hello World") which it does something with.
Note that we tell code from variables because code is always followed by parentheses, even if there is no data involved.
Why and how would we do this?
Why
We divide code between classes to match a key idea in Object Orientated design: Encapsulation. One aspect of this is that data and the methods to work on it should be in the same class so they are easily added or replaced. Classes should deal with just one thing. This means we must have some way of running code in other classes.
Within a class we want to separate the code into different functional units:
enhances encapsulation by providing toolkits of code to deal with specific data;
easier to maintain;
easier to check.
Also means that if we want to repeat code in different places (where a loop wouldn't help), we can just call the chunk of code.
How?
Such a chunk of code is called a method.
We've already used one:
System.out.println("Hello World");
And, more surprisingly, we've already written one ourselves, the 'main' method:
public static void main (String args[]) {
}
Code structure
Almost all of the code in classes must be within methods.
Methods cannot be nested (easily).
Code runs by processing a series of methods.
Each method is called by another method in turn, or, in the case of user interaction, by methods in the JVM.
Processing can't magically skip to methods that aren't called - calling methods is the only thing that structures the code execution at this scale - if you want a method to run, you have to call it from somewhere.
Example use
public class HelloWorld {
public static void main (String args[]) {
System.out.println("Hello World");
}
}
This calls the println method and runs it.
The println method almost certainly calls other methods inside other classes we didn't write in order to do its job.
(don't worry about these classes for the moment - you get these invisibly and for free with the java language, like System)
Processing cascades through the program by methods calling other methods in other classes.
Example method
public class Point {
double x = 0.0;
void incrementX() {
x++;
}
}
void incrementX() is the method declaration.
Variable names are lower-case starting camelCase.
We'll come back to void.
Note that variables set up when this class is turned into an instance (i.e. an object) are the only running code outside of methods.
This means their scope is throughout the code, including the method.
Using our example
public class PointUser {
public static void main (String[]args) {
Point pt1 = new Point();
pt1.incrementX();
}
}
pt1.incrementX() is the method call.
Note that we usually have to make an object to use either variables or methods inside them (we'll come back to System).
Note that the x variable within the pt1 object is created when it is instantiated.
Code structuring
Classes are composed of instance variables and one or more sets of unnested methods:
public class Point {
double x = 0.0;
double y = 0.0;
void incrementX {
x++;
}
void incrementY {
y++;
}
}
Methods
Methods are like tools in a toolkit.
In addition to just running code, as here, they can also take in and return data.
In previous slides, void in the declaration means nothing is returned.
Return values
public class Point {
double x = 0.0;
boolean isXGreaterThanZero() {
if (x > 0) {
return true;
} else {
return false;
}
}
}
The return type of the method must match the thing returned.
All paths through the method must return something, or the compiler will complain.
Example use
public class PointUser {
public static void main (String[] args) {
Point pt1 = new Point();
boolean answer;
answer = pt1.isXGreaterThanZero();
}
}
Good returns
Because something is being returned, you have to do something with it: