09-19-2013

posted Sep 20, 2013, 7:36 AM by Samuel Konstantinovich   [ updated Sep 20, 2013, 7:40 AM ]
Classes from the demo:

public class driver{

    public static void main(String[]args){
        //create an instance of the greeter class, in variable R
        greeter R= new greeter();
        //create an instance of the greeter class, in variable Arrr
        greeter Arrr= new greeter();

        //change the message of Arrr to "Matey!"
        Arrr.setMessage("Matey!");

        //methods that get information from the class are much more useful than 
        //methods that print. If you want to make a method to print, 
        //you should consider returning a string instead.
        System.out.println(R.getMessage());
        System.out.println(Arrr.getMessage());
        //This is driver class contains the the main method, so it should decide when to print and when not to. 
    }

public class greeter{
    //instance variables
    private String message;

    //this is the constructor
    public greeter(){
        //constructors will initialize the object to a state such that it can be used. 
        message = "Hi";  
    }

    //mutator method, or setter
    public void setMessage(String s){
        message = s;
    }

    //accessor method, or getter
    public String getMessage(){
        return message;
    }

}



Comments