Flow control:
methods and classes

Dr Andy Evans

[Fullscreen]

The story so far...

  • Classes act as photocopy originals from which we make objects.
  • The most important class is the one we pass to the interpreter. It must have a 'main' method.
  • To make a variable we make the label in memory, then stuff something in it.
  • We can make an object variable using a particular class:
    	
    ClassName objectName = new ClassName();
    
    

Referring to methods

  • So far the methods have been outside our class.
  • If they are in the same class, you don't need an object:
    	
    public class A {
    
        void method1() {
    		method2();
        }
    
        void method2() {
    		System.out.println("done");
        }
    }
    
    

Constructors

  • Special method for setting up objects. Method calls aren't very efficient, so we want to minimise them within reason.
  • Usually first method in any class. If they're not there (we haven't made any so far), the compiler makes them:
    	
    public class Point {
    
        double x = 0.0;
    	
        public Point () {
    		x = 10.0;
        }
    }
    
    
  • Return an object of that type and have no name (or have no name, and implicitly return that type).
  • Called when we make a new object:
    	
    Point pointObject = new Point ();
    
    

Constructors

  • Nothing to stop us passing stuff in if we write the constructor to take it.
    	
    public class Point {
    
        double x = 0.0;
    	
        public Point (double xIn) {
    		x = xIn;
        }
    }
    
    
  • When we make a new object:
    	
    Point pointObject = new Point (10.0);
    
    

Polymorphism

  • The constructor can be overloaded to set variables differently.
    
    public class Point {
        int x = 0; 
        int y = 0;
    	
        public Point () { 
    		x = 10.0; 
    		y = 20.0;
        }
    	
        public Point (int startX, int startY) {
    		x = startX; 
    		y = startY;
        }
    }
    
    
  • Called thus...
    
    Point point1 = new Point();
    Point point2 = new Point(23, 42);
    
    

Static

  • Note that generally we have to make objects in order to use their variables / methods.
  • But this isn't always the case. Some methods and objects can be used without creating them directly, as in the variable System.out.

Static

  • This was set up with the static keyword:
    
    public static PrintStream out;
    
    
  • Static variables/methods are used directly from the class.
  • There is one copy that can be used anywhere. Change static variables in one object, and the variable changes in all.
  • Static variables/methods are used directly from the class.
  • There is one copy that can be used anywhere. Change static variables in one object, and the variable changes in all.
  • Static methods can only use other static variables and methods.

Static examples

  • Static methods are commonly used to make toolkits.
  • For example, the System class, and the Math class:
    	
    double randomNumber = Math.random();
    double ans = Math.sqrt(9);
    
    
  • These often also contain variables set as final and static, so they can be used without objects, but can’t be changed:
    	
    Math.PI
    
    

Main

  • We can now understand the main declaration:
    	
    public static void main (String args[])
    
    
  • public : so the JVM can use it.
  • static : so it can work without an object being made.
  • void : it returns nothing.
  • String args[] : it takes in an array of Strings.

String args[]

  • For command line arguments.
  • Takes in a String array and calls it 'args'.
  • i.e., you can start the program like this...
    	
    > java programName hi little program -10
    args[0] == "hi"		
    args[1] == "little"
    args[2] == "program" 	
    args[3] == "-10"
    
    
  • Note that the resulting values are Strings, even if representing numbers.

Main issues

  • Main's static nature cause problems.
  • It is usual to avoid this by doing everything in the main class in a constructor:
    	
    public class PointUser {
    
        public PointUser () {
    		// Stuff done here.
        }
    
        public static void main (String[] args) {
    		PointUser ptUsr = new PointUser();
    		// or just 'new PointUser();'
        }
    
    }
    
    

Review

  • Constructors are super useful for cutting out unnecessary method calls.
  • Best way to avoid the issues with main being static is:
	
public class PointUser {    

    public PointUser () {		
		// Stuff done here.    
    }    
	
    public static void main (String[] args) {		
		new PointUser();		
    }
}