Escolar Documentos
Profissional Documentos
Cultura Documentos
In this lecture...
We will examine: Java classes and their members
in some detail Static variables Static methods The importance of Java classes for Java programming Instance variables and methods Class constructors
Classes:
Classes have variables and methods. All methods are defined inside classes a method always belongs to something! Java class library (the Application Programming Interfaces or APIs) - over 2000 classes.
Examples:
Input/Output (I/O)
Networking
Defining a Class
All the examples done up to now have involved
defining a class! We have either defined a class with a main method, to enable us to test some feature of Java., or we have defined an applet Defining a class is done in a completely standardized way
subclasses An example (from biology): The class Mammal could have a superclass Animal and subclasses Lion and Tiger
Basics of a Class
A class has a name A class contains zero or more field members
(variables)
A class contains zero or more method
case
The second part is the keyword class
in this case
8
What is a modifier?
This is a keyword used in the definition of some
element of Java which in some way modifies the characteristics of that element, whether it be a class, a variable or a method
10
where the class body is defined (contained between braces curly brackets). Basically the body is everything except the first line of the class. In our previous examples, we have either defined a main() method (for applications) or a paint() method (for applets). So these classes have had just one member a method. Most classes have several method members (as well as field members)
11
purposes.
Fields store state Methods represent behaviour
12
the class body They are basically variables that hold data which record the current state They are sometimes called properties or attributes
13
15
16
17
Field Modifiers
static final
18
19
explicit modifier) public means accessible to everything private means accessible only to the class. In general, it is best to make field variables private, so that they are not accessible outside the class. This is an example of encapsulation protected means accessible to all classes in the same package (packages are discussed later) and subclasses of the class default means accessible to all classes in the same package
same class, we do not have to actually specify what class the field belongs to it is assumed we are referring to the current class If we want to, we can make the reference explicit. The current class can be referred to as this (the current thing) so we can refer to a field a as this.a
20
default value. Example: int x; Numerical fields have default value of zero, boolean fields have false assigned, char fields have \u0000 assigned (which means the character whose value is coded as 0), reference variables are assigned null Another way is to explicitly assign a value. Example: int x = 99; A third way is to use a static initialisation block
21
Method Members
public class FieldDemo { public static int x = 4; public static void main(String[] args) { System.out.println("x has value: " + x); } }
22
specific action In other languages, methods are sometimes called functions or procedures When a method is called (invoked) data may be passed to it. Some action is performed, and a data item might be returned from the method a returned value
23
Static Methods
We will here look at static methods
24
explicit modifier) In general, it is best to make methods public , so that they are accessible (exposed) outside the class The exception would be some method which is only used within the class. In this case, it could be made private, to encapsulate it The other 2 accessibility modifiers may be used to control accessibility where we have inheritance
26
method returns to its calling method A single value may be returned It may be of any type If no value is to be returned, the return type is described as void The return key word is used to indicate what is returned
27
arguments passed into the method) Examples: public static void main(String [] args)
public static adder (int a, double b)
In the first example, there is one parameter, a
need the parentheses () You can use any (legal) names you like for the parameters The parameter values are passed in by copying This method of passing data in is called pass by value or call by value
29
30
Method Example 1
public class MethodExample1 { public static void method1 () { System.out.println( "Hello from method1"); } public static void main(String[] args) { this.method1(); }}
31
32
called with an int value, the actual value passed into the method is broadened into a double. There is no problem filling a 64 bit space with a 32 bit int value.
This occurs automatically, and usually does
33
MethodExample2
public class MethodExample2 { public static void method2 (double x) { System.out.println("Twice " + x + " is " + 2 * x ); } public static void main(String[] args) { method2(6); } }
34
35
MethodExample3
public class MethodExample3 { public static void method3 (int x) { System.out.println("Twice " + x + " is " + 2 * x ); } public static void main(String[] args) { method3(6.0); } }
36
37
Running MainExample
We could launch it from the command line
Tools to set up EditPlus so that when Java is run, the system does a Prompt for arguments. In this case, input fred jim when prompted
40
41
hold members (methods and fields) as a way to do simple Java programming In object-oriented programming, a class is used mainly to describe a type of object. Having defined a class, we can use it to construct objects of that type Example: we may describe a Button class, and then create (instantiate) individual Button objects
42
of field data representing state and methods representing behaviour, all for one entity
An object encapsulates its fields and methods,
exposing only what is to be used by other classes and objects its public interface
Designing a program involves defining classes
43
and the relationships between classes, and creating suitable class packages for deployment
which will assign values to instance variables We might assign default values to some instance variables The following example shows both
44
MyClass2 (int a, int b) { x = a; // could use this.a for clarity y = b; } // could use this.b for clarity
45
47
with no arguments, one is automatically created) If any other constructor is defined, this automatic no-args constructor is not available If you then want a no-args constructor, you have to define it explicitly
48
Summary
We have looked at Java classes and their
members in some detail Static variables Static methods The importance of classes Instance variables and methods Class constructors Useful classes from the Java APIs
50