CSCI A202 - Introduction to Programming (II)

Lecture 2: More BreezyGUI; Scope and visibility of variables (last updated Jan 14)

These are notes, under development. The purpose is to collect the code that will be written on the blackboard, and explained in class. These notes are brief and sketchy, at least for the time being.

We want to talk about the scope and visibility of variables in running programs without using any object oriented terminology (no instance or class variables, we'll use them soon, but not right now).

We'll declare ourselves happy if we can identify a few rules that we can use to reason about the structure of programs (and in particular we want to master the rules for scope and visibility of variables, now) so well that we can predict with great accuracy what the output of a certain program will be, when run, before we actually go and run the program.

The main point, the original question, was:

In FahrenheitToCentigrade how do we know (at this stage in the course) that buttonClicked has access to centigrade, and can modify it and can pass it to degreesCentigradeField's setNumber ?
(This also serves as a brief introduction for those of you that have taken A201 either too long ago or in a C++ oriented environment).

So we want to find, first, the simplest program that can be written, compiled, and run in Java so that we can start using variables with it first (being simple) in order to understand how we determine their visibility and how variables can be used.

The empty file compiles fine but can't be run. No class is found.

If we define a(n empty) class inside no main method is found.

We define a(n empty) main method in our class and the program compiles and runs fine, and when executed it does what it is expected to do, that is, nothing.

Now we are interested with issues of visibility and scope of variables.

public class One {
  public static void main(String[] args) {
 
  } 
}
We note that this is probably the shortest (and simplest) standalone application that we can write. We want to keep the details at a minimum.

Let's note we are carrying with us the

String[] args
part, in the definition of main, even though we don't use it. (We barely use anything at all, in fact).

Let's summarize what we have to have for a running program:

Now we want to look at a working program, a program that does some work not just runs. Sooner or later we'll get into variables. In Java variables have types and they need to be announced, declared, in advance (before they are used). This means we need to do two things: In addition to main we can define other methods in our class.

They could use each other.

They're peer methods, they see each other, they can call each other (modulo a few observations now and a little more later in the semester).

This can get complicated easily (and in many ways) but to keep it simple:

public class One {
  public static void main(String[] args) {
    String message = myFunction();  
    System.out.println(message); 
  }
  static String myFunction() {
    return "Hello world!"; 
  } 
}
is just another way to write the "Hello, world!" program.

The only thing that we'll explain away for now is static.

(In fact we'd better get used to it).

Now let's use variables.

We did use one, message, but that's because we wanted to rewrite in slow motion the statement

System.out.println(myFunction())
which would have otherwise used no variables at all.

Let's look at this program:

public class One {
  static String message;  
  public static void main(String[] args) {
    message = myFunction();  
    System.out.println(message); 
  }
  static String myFunction() {
    return "Hello world!"; 
  } 
}
It's the same program, only message has moved and turned blue.

This is the point where we explain visiblity (in class) and insist some more on allowing static to stay there for a while, at least until we understand its purpose.

Circles, enclosures, getting out through the parameter list etc.

Mostly int's are used etc. (In fact in class we used Strings).

Global variables, as opposed to local.

But this is only an approximate rule, although a faithful one.

We'll have a more accurate description in hand when we finish chapter 8.

So that's how buttonClicked can use centigrade to put some data into it

But note that after main is run control is with a different system. You don't see that system. You have access to few parts of it. You have to trust that system for now, and simply remember that you can customize it by redefining methods such as buttonClicked (whose names you cannot come up with, but are being made available to you).

By contrast in our "Hello, world!" programs control never leaves our sight.

That's because the "Hello, world!" programs are sequential in the most predictable way - the user (of the program) has little, if any, input in what happens.

So we can have two kind of variables, roughly speaking

and we can use them to interact with the user and process the information inside the program too (main initializes the window objects (implicitly) but they cannot be declared inside main (they can only be initialized there, if we so much want it) because they would become invisible to buttonClicked etc. And on top of it we'd have problems with main being static again and we said we would not explain the meaning of static right now because we'd have to use terminology that we want to avoid (for now).

We'd have to use a constructor etc. and that's where we'd get around the static issue and the visibility aspect will remain the only factor.

That is,

import java.awt.*;
import BreezyGUI.*; 
public class One extends GBFrame {
  public One() {
    Button b = addButton("clickMe", 2, 1, 1, 1); 
    Label  a = addLabel ("myLabel", 1, 1, 1, 1); 
  }  
  public static void main(String[] args) {
    // Button b = addButton("Text", 1, 1, 1, 1); // non-static addButton 
    Frame f = new One(); 
    f.setSize(100, 100); a.setText("Hello."); 
    f.setVisible(true);
  }
}
does not compile
tucotuco.cs.indiana.edu% javac One.java
One.java:11: Undefined variable or class name: a
    f.setSize(100, 100); a.setText("Hello."); 
                         ^
1 error
tucotuco.cs.indiana.edu% 
(although if you don't use a label at all you still get a messageBox but that's getting way outside of what we can talk about now).

This is where we will start our lecture, and discuss:

There's no point in printing this page right now as it will be changed and updated soon. We'll let you know when you can print it, if you want to, for your reference.