Objects

Dr Andy Evans

[Fullscreen]

Review

  • Object Orientated programs can be made of several objects which do different jobs and communicate with each other.
  • In the introduction we looked at this example of objects and we said each object had its own file.
  • So how do these objects relate to what we have talked about here?

Classes

  • We have seen that the basic unit of code is the class.
  • We have one class that sets everything running. But how do we run the others?
  • The main class is unusual in running directly as a program.

 

  • More usual to think of classes as chunks of code that define a type.
  • We use them to make ("instantiate") specific Objects of that type - a bit like an original of a photocopy.
  • These are variables that contain the class code.

 

  • This is the real genius of Object Orientated Programming - we can have variables that contain other code, e.g. the code to make a Menu.

Objects

  • Because they are based on classes, objects can contain any code classes can.
  • Code to do jobs.
  • Code to communicate with other code.
  • Code to store data - which we'll concentrate on in this section.

Creating Objects

  • Objects are just variables with a type (class) you can define yourself.
  • Let's make a class for storing a geographical point:
    	
    public class Point {
    	double x = 100.0;
    	double y = 200.0;
    }
    
    
  • Note that there's no main block as we're going to use this class inside a main block of another class which is in the same directory.
  • This class just contains some initialised variables.

 

Creating Objects

  • In the main block, you tell the computer to make a name-tag ready for the new object, e.g.,
    	
    Point home;
    
    
  • Then attach it to a new object of this class.
    	
    home = new Point();
    
    
  • Or all in one go...
    	
    Point home = new Point();
    
    
  • Note, again, the capitalisation. Contrast this with the primitive variables.
    	
    double x = 100.0;
    
    

Accessing stuff in objects

  • You can use the dot operator '.' to access things inside the objects.
  • You can imagine it means "look inside and get". So, in our main class, after making the object, we might have:
    	
    double myX = home.x;
    
    
    to get the object's x variable or
    	
    home.x = 200.0;
    
    
    to set it to some value.
  • Each object gets a complete copy of the class code for itself. Change a variable in one object and it usually just changes there, not for all objects of the same class.

Finished program

	
public class HomeFire{

    public static void main(String args[]) {
	
		Point home = new Point();
		home.x = 23.42;
		home.y = 79.79;
		System.out.println("x:" + home.x);
		
    }
	
}

Obviously this isn't the most exciting program in the world, but you get the idea.

Running our program

We have more than one file to compile, so how do we go about it?

 

  1. We put the Point.java and HomeFire.java files in the same directory.
  2. We compile them using the "all files" wildcard "*":
    	
    javac *.java
    
    
  3. We then run the main file:
    	
    java HomeFire
    
    

     

    The compiler / JVM work out all the links for us.

Some objects revised

	
Menu fileMenu = new Menu ("File");

MenuItem saveWeb = 
		new MenuItem ("Save as Web Page...");
		
fileMenu.add(saveWeb);

MenuListener a = new MenuListener(saveWeb);

Some objects revised

  • If there is code inside the object, it can be run the same way - but we'll come back to that.
  • We've actually seen a kind of example already:
    	
    System.out.println("Hello World");
    
    
    but this is special, because we don't need to make the System class into an object first. We'll come back to this as well.

Review

  • The take home message is the same as for primitives:
    	
    Point p1; 
    
    
    declares/creates an Point label, called p1.
    	
    p1 = new Point();
    
    
    assigns it to a new Point-type object.
  • You can do both at once, thus:
    	
    Point p1 = new Point();
    
    
    But from then on, you just use the label, thus:
    	
    p1.x = 200.0;
    
    

Assignment to variables

  • The major difference between objects and primitives is what happens when two labels are set to each other:
    	
    double a = 10.0;
    double b = a;
    
    
  • With primitives, the value is copied. So,
    	
    a = 20.0;
    
    
  • Will change a, but not b.

Assignment to variables

  • Whereas with objects, the two labels are stuck to the same object, and
    	
    Point p1 = new Point();
    Point p2 = p1;
    a = 20.0;
    
    
    will change both p2.x and p1.x, because they are labels for the same object.
  • Confusing the two results in tricky to find issues. To copy an object you need to copy all its primitive contents separately.
  • To copy an object you need to copy all its primitive contents separately.
  • There are techniques for helping with this (search for 'cloning' and 'java'), but they are tricky to implement well, so we won't cover them in this basic course.

Null

  • Numerical primitives are automatically set as zero unless assigned (though you should also do it).
    	
    int a;
    int a = 0;
    int a = 42;
    
    
  • Object labels are set to null unless attached to an object.
    	
    ClassName a;
    ClassName a = null;
    ClassName a = new ClassName();
    
    

Scope

  • Variables labels usually only work in their own blocks and any blocks nested within their blocks.
  • Labels are destroyed and rebuilt each time a scope is left and re-entered. Note that once a object has no label, it's useless.
  • This wouldn't work:
    	
    {
        Point a = new Point();
    }
    System.out.println(a.x);
    
    
  • Whereas this would, and isn't unusual:
    	
    Point a = null;
    {
        a = new Point();
    }
    System.out.println(a.x);
    
    

Strings

  • We saw earlier that the primitive for storing text characters is char.
  • However, there is a special class called String that can store more than one character. They are created for you, without having to use new:
    	
    String hw = "Hello World";
    System.out.println(hw);
    
    
  • As with text more generally, if you do this you can't break in the middle of the quote-marked line (unlike the rest of Java - see details on the site).

Next

Holding lots of data of the same type.