English Regions (Passing Object Variables)

Normandy House

When William conquered England an interesting question arose. Was Normandy now part of England? It was a question that would bring constant strife between England and France for centuries. England would insist that parts of France belonged to their nation. Even in the Conquerer's time William would be constantly fighting battles on the mainland.

The code shown in the following files demonstrates the passing of an object variable from one object to another. In reality the methods used actually create a pointer to the object variable.

  1. class England {
  2. int numregions;
  3. }

This code calls the object class that holds a variable that is used by methods within the calling class.

Java code

  1. import static java.lang.System.out;
  2. public class EnglishRegions {
  3. public static void main(String[] args) {
  4. England t1 = new England();
  5. t1.numregions = 7;
  6. totregions(t1);
  7. out.println("Prior to the Norman conquest England had 7 distinct regions.");
  8. out.println("When Normandy was added the count changed to: " + t1.numregions);
  9. out.println("Here is a list of the regions:");
  10. out.println("Northumbria, Mercia, East Anglia, Essex, Sussex, Wessex, Kent, Normandy.");
  11. }
  12. static void totregions(England aEngland) {
  13. aEngland.numregions++;
  14. }
  15. }

Java code

Use Save As to save the code files to your folder. Then compile the code files in the Command prompt window. Run the EnglandRegions.java file.

Your output should look like this:

C:\javamike>java EnglishRegions

Prior to the Norman conquest England had 7 distinct regions.

When Normandy was added the count changed to: 8

Here is a list of the regions:

Northumbria, Mercia, East Anglia, Essex, Sussex, Wessex, Kent, Normandy.

 

< Windows (API Objects) Knights (Array Initializer) >