- •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.5. Scope of an identifier. Local variables. Access control.
The scope of an identifier is the region of the program within which it can be referred to using a simple name.
Class members (fields and methods) can be referred to from any method of the class, no matter where in the class body the declaration of the member occurs. A field initializer can refer only to the fields of the class which were declared earlier.
Each method may declare its own variables (or constants). The scope of their identifiers is local - it starts at the place of declaration and ends with the method body (at the closing brace of the method). Such variables are called local variables. Method parameters have the same scope. In fact, parameters are local variables. The scope of a variable declared inside the local block (set of instructions enclosed with braces) is the rest of the block in which the declaration appears. Consider the following class:
class A {
int a;
void method1() {
int b;
...
}
void method2() {
int c;
...
}
}
Inside the method method1 we can access the variables a and b and the method method2. Inside the method method2 we can access the variables a and c and the method method1. The variable c cannot be referred to inside the method method1, and similarly the variable b cannot be referred to inside the method method2. Inside a constructor or a method a class field identifier may be shadowed. For example:
class A {
int a;
void method() {
int a = 0; // the field a is shadowed by the local variable a
a = a + 10; // the local variable a is used here
this.a++; // the class field is used here
}
}
The local variable x is declared inside the method method(). It has the same name as the class field, therefore the local variable shadows the class field. The expression a in the method() refers to (denotes) the value of the local variable. The class field may be accessed using the qualified name: this.a.
In Java local variables cannot be shadowed in local blocks.
Thus the definition: class A { .... void method() { int a; { int a; ... } } } is erroneous. Contrary to class variables (fields), local variables are not automatically initialized. The value of a local variable is unspecified until it is initialized by an initializer or assignment.
Local variables should be explicitly initialized.
What about accessing members of a class from another class? It is determined by access modifiers. The accessibility of a class member may be:
private - it is accessible only inside the class it is declared (the modifier private)
default - it is accessible from within the classes defined in the same package (no modifier required)
protected - the member is accessible from within the class, all its subclasses and classes defined in the same package (the modifier protected)
public - the member is always accessible (the modifier public)
We have already learned some of the reasons for using access modifiers. Note how they restrict access to class members:
import javax.swing.*;
public class Pair {
private int a;
private int b;
public String name;
public Pair(int x, int y) {
a = x;
b = y;
}
private String makeString() {
return name + " " + a + " and " + b;
}
public void show() {
JOptionPane.showMessageDialog(null, makeString()); // 6
}
}
class Test {
public static void main(String[] args) {
Pair p = new Pair(17,20);
p.name = "Pair of numbers"; // 1
p.a = 1; // 3
System.out.println(p.makeString()); // 4
p.show(); // 5
}
}
Compilation generates the following errors:
Pair.java:29: a has private access in Pair p.a = 1; // 3 ^ Pair.java:30: makeString() has private access in Pair System.out.println(p.makeString()); // 4 ^ 2 errors
Note that:
the line denoted with // 1 is correct, because the field name is declared public
the lines denoted with // 3 and // 4 generate errors as the fields declared private cannot be accessed from within other class
the line denoted with // 5 is correct, because we access the public method
the line denoted with // 6 is correct too, because we access the private member from within the class in which it was defined
Classes may be declared public. Such classes may be accessed by any code. If a class is not declared public, then it may be accessed only from within the package in which it is declared. To declare a public class the modifier public must be used: public class Pair { ... }
A source file may contain at most one public class. The name of this class must be identical as the name of the file.
