250 likes | 551 Views
Building Java Classes. Class Definition Standard Methods Static Variables and Methods Robert Stumpf, Ph.D. CIS Department. Class Definition. A Java class is simply specified by: class <name> { private <type> <name>; // attribute public <type> <name ( ) // method { } }.
E N D
Building Java Classes • Class Definition • Standard Methods • Static Variables and Methods Robert Stumpf, Ph.D. CIS Department cs141_03 - Building Java Classes
Class Definition • A Java class is simply specified by: class <name> { private <type> <name>; // attribute public <type> <name ( ) // method { } } cs141_03 - Building Java Classes
Class Definition • A Java class example: class Temperature { private int celsius; // attributepublic int getCelsius ( ) // accessor {return celsius;} public void setCelsius (aCelsius) // mutator {celsius = aCelsius;} } cs141_03 - Building Java Classes
Class Definition • Getters are called accessors public int getCelsius ( ) { return celsius; } • Setters are called mutators public void setCelsius (aCelsius) { celsius = aCelsius; } cs141_03 - Building Java Classes
Class Definition Naming of arguments: • Your instructor uses the ‘a’ or ‘an’ before the name • This facilitates ease of reading code • Resolves conflicts when local variables might have the same name • For example: aCelsius and celsius • The default for IBM Visual Age is acceptable (newValue) cs141_03 - Building Java Classes
Class Definition • Testing the temperature example class : int celsius; // declaration Temperature temperature; // declaration temperature = new Temperature ( ); // instantiation temperature.setCelsius (20); // populate celsius = temperature.getCelsius ( ); // retrieve • Note a default constructor exists called Temperature ( ) cs141_03 - Building Java Classes
Standard Methods • In addition to the default constructor one may create additional constructors • Constructors allow one to instantiate and populate a class in one statement rather that having to perform mutations to the attributes after instantiation • One can then instantiate and populate a new temperature without having to use a mutator cs141_03 - Building Java Classes
Standard Methods • A Java constructor example (with no parameters): private int celsius; Temperature ( ) { celsius = 0; } • Constructor has the same name as the class cs141_03 - Building Java Classes
Standard Methods • A Java constructor example (with parameters): private int celsius; Temperature ( int aCelsuis) { celsius = aCelsuis; } • This constructor is more convenient as it both instantiates and populates cs141_03 - Building Java Classes
Standard Methods • It is standard procedure to build a constructor for each class that populates all of its instance variables • If a class has ten instance variables the constructor will then have ten arguments • The exception to this is date instance variables which are often set to today's date from the system cs141_03 - Building Java Classes
Standard Methods • In addition to constructors there is a toString • A toString allows one to obtain a printable copy of the object for display purposes • It is standard to always include a toString • The inherited toString delivers a cryptic string so the designer must write their own cs141_03 - Building Java Classes
Standard Methods • A toString converts object to a string: class Temperature { private int celsius; public String toString ( ) { return Integer.toString (celsius) + " o C"; } } • Returns "20 0 C" if value was 20 cs141_03 - Building Java Classes
Standard Methods • A toString that includes the class name: class Temperature { private int celsius; public String toString ( ) { return this.getClass ( ) + + Integer.toString (celsius); } } • Returns "package.Temperature 20" cs141_03 - Building Java Classes
Standard Methods • Testing the Java toString example:String string; Temperature temperature; temperature = new Temperature (20); string = temperature.toString ( ); System.out.println(string); System.out.println(temperature); // Gives the same answer • Note that either using the toString explicitly or implicitly works equally well cs141_03 - Building Java Classes
Static Variables & Methods • A Java static (class) variable example: class Temperature { private int celsius; private static int instanceCount; Temperature (int aCelsius) { celsius = aCelsius; instanceCount + +; } } cs141_03 - Building Java Classes
Static Variables & Methods • A Java static (class) method example: public static int getInstanceCount ( ) { return instanceCount; } • Note that there is only one value of instanceCount per class even if there are many instances • Note the word ‘static’ in method declaration cs141_03 - Building Java Classes
Static Variables & Methods • Testing the Java static variable example: int celsius, numberInstances; Temperature temperature ; temperature = new Temperature (20); celsius = temperature.getCelsius ( ); numberInstances = Temperature.getInstanceCount ( ); • Note that the static method is sent to the class name cs141_03 - Building Java Classes
Cloning Classes • The ability to copy an object is needed • This is because Java uses pointers to objects • For example: Temperature t1 = new Temperature (20); Temperature t2 = t1; • This results in only one object and both variables are referencing it cs141_03 - Building Java Classes
Cloning Classes • What needs to be done is: Temperature t1 = new Temperature (20); Temperature t2 = (Temperature) t1.clone ( ); • (Temperature) is a cast (a conversion from an object to Temperature) • clone ( ) method must be provided by the programmer cs141_03 - Building Java Classes
Cloning Classes • A typical clone method: public Object clone ( ) throws CloneNotSupportedException { Temperature copy; copy = (Temperature) super.clone ( ); return copy; } • Note that this method overrides the parents method cs141_03 - Building Java Classes
Cloning Classes • Notes on cloning: • The actual method should allow for exceptions using try and catch • This, however, is a later topic • Note that super refers to parent class of Object cs141_03 - Building Java Classes
Summary • Classes are needed for model objects • Classes can also be used to extend GUI classes • Or one may develop or extend the java library • An example of this might be to build a better toString for GregorianCalendar cs141_03 - Building Java Classes
Summary • Occasionally static variables and methods are needed for such things as counters • Standard methods are expected in your business object model • Constructors • Accessors and Mutators • toString ( ) • clone ( ) // Sometimes cs141_03 - Building Java Classes
Thank You “A little in the right direction can make a big difference.”Thank you Robert Stumpf email: rvstumpf@csupomona.edu url: http://www.csupomona.edu/~rvstumpf cs141_03 - Building Java Classes