- •Lecture 1 Programs and algorithms
- •1.1. How a computer operates?
- •1.2. Algorithms and programming languages
- •If (condition) then ... Or if (condition) then ... Else ...
- •1.3. Summary
- •1.4. Exercises
- •Lecture 2 Fundamentals of programming I
- •2.1. The language
- •2.2. Quick start: first program and basics of syntax
- •2.3. Data in a program (variables and literals)
- •2.4. Operations on data (operators and expressions)
- •2.5. Summary
- •2.6. Exercises
- •Lecture 3 Fundamentals of programming II
- •3.1. Making decisions
- •3.2. Iterations
- •3.3. Arrays
- •3.4. Functions
- •3.5. Summary
- •3.6. Exercises
- •Lecture 4 Java and object-oriented methodology
- •4.1. What is Java
- •4.1.1 Java as a universal programming language
- •4.1.2. Java as a cross-platform language
- •4.1.3. Java as a universal environment for gui programming
- •4.1.4. Java as a universal environment for accessing data bases
- •4.1.5. Java as a universal multimedia programming environment
- •4.1.6. Java as a universal means for client-server programming
- •4.1.7. Java in a distributed environment
- •4.1.8. Java as an environment for building applications from ready-to-use components.
- •4.1.9. Java as the environment for xml processing
- •4.1.10. Micro Java
- •4.1.11. Why is Java worth learning?
- •4.2. Introduction to objects
- •4.4. The first program
- •5.2. Literals
- •5.3. Types of variables. Declarations.
- •Type_name variable_name;
- •Identifiers
- •Naming conventions:
- •5.4. More on operators and expressions
- •5.5. Numeric promotions
- •5.6. Summary
- •5.7. Exercises
- •Lecture 6 Objects
- •6.1. Objects and references
- •6.2. The class String
- •6.3. Useful examples
- •6.4. Summary
- •7.2. Defining attributes of objects
- •7.3. Defining operations on objects (methods)
- •7.4 Defining methods of object creation (constructors)
- •7.5. Example
- •7.6. Inheritance
- •7.7. Summary
- •7.8. Exercises
- •Lecture 8 Classes II
- •8.1. Accessing class members. The variable this.
- •8.2. Static members
- •AClassName.AFieldName
- •8.3. Explicit initialization
- •8.4. Packages and imports
- •8.5. Scope of an identifier. Local variables. Access control.
- •8.6. Structure of a program. Running an application.
- •8.7. Summary
- •8.8. Exercises
- •Lecture 9 Decisions
- •9.1. A brief survey of control statements.
- •9.2. Comparison operators and expressions
- •9.3. Logical operators and expressions
- •9.4. Making decisions: the if and if-else statements.
- •9.5. Multivariant selections done with the switch statement.
- •9.6. The conditional operator ?:
- •9.7. Summary
- •9.8. Exercises
8.2. Static members
Members are fields and methods of a class.
Fields and methods of a class may be static or non-static. Until now we have used only non-static members. Non-static members are always connected with an existing object (fields represent data stored in the object, methods are messages sent to the object and must be called for the object).
Static members (fields and methods):
are declared with the modifier static
may be used even when no object of the class exists
Static members may be accessed with the class name followed by the dot and the field name:
AClassName.AFieldName
In the class System (contained in J2SDK) there is static method exit(int) which terminates the execution of the application (and returns the given return code). Thus, to terminate the program with the exit code 0 we write:
System.exit(0);
Earlier we have used methods displaying dialogs: JOptionPane.showInputDialog("Give some data") JOptiomPane.showMessageDialog(null, "Message") They are static methods of the class JOptionPane. Thanks to this, we can call them using the class name - JOptionPane. The method parseInt, which we used to convert strings to numbers, is a static method of the class Integer. Therefore we wrote: Integer.parseInt("123"); Now the most confusing puzzle. The class System has a static field named out which specifies the standard output of the program (the command prompt). This object understands the message println which causes printing the argument as a string to the command prompt (println is the method of the class PrintStream; out is the object of the class PrintStream). So we write System.out.println(...)..
Only the variables which are fields of classes may be declared static.
It is very important to realize the difference between static and non-static members. Static members describe properties of the class, not its objects. Particularly, in the case of fields the difference is as follows:
non-static fields exist (as components) in each object of the class
static fields are not contained in objects; each static variable occupies a single memory cell for the whole class of objects.
For example, in a class describing hard disks we have a static field named vat and non-static fields: model, capacity, price. Disks differ in model, capacity and price but have the same value of the VAT, so there is no need for storing it in each object separately. It is a common property of all disks. Being a static variable it occupies only 8 bytes of memory space regardless of the number of disks (objects) created in the program.
public class Disk {
private static double vat;
private String model;
private int capacity;
private double price;
public Disk(String m, int c, double p) {
model = m;
capacity = c;
price = p;
}
public String getDescription() {
return model + " ," + capacity + " MB";
}
public double getGrossPrice() {
return price * ( 1 + vat/100 );
}
public static void setVat(double v) {
vat = v;
}
}
class Test {
public static void main(String[] args) {
Disk.setVat(22.0);
Disk d1 = new Disk("IBM598", 6040, 430.0);
System.out.println(d1.getDescription() + " - price "
+ d1.getGrossPrice() + " EUR");
}
}
IBM598 ,6040 MB - price 524.6 EUR
Note that the method setVat had been called before any object was created. It set the value of the field vat, although no object of the class Disk existed.
Static methods cannot access non-static members of the class (as no object may exist). Other static members may be accessed.
O f course, non-static methods may access static members in a normal way (see the method getGrossPrice()).
