170 likes | 330 Views
Non-Static Classes. What is the Object of this lecture?. Non-Static Methods. We have made our own static methods, now it is time to make your own non-static methods. In other words, we will make our own objects and methods which operate upon them. Like Random rand = new Random( );
E N D
Non-Static Classes What is the Object of this lecture?
Non-Static Methods • We have made our own static methods, now it is time to make your own non-static methods. • In other words, we will make our own objects and methods which operate upon them. • Like Random rand = new Random( ); rand.nextInt( );
Constructors • Constructors are basically methods which create objects. • You can recognize a constructor because it has the same name as the class it is in. • This is because it constructs an object of that class.
Constructor Syntax • public class Name { public Name( ) { … code…. } }
Object Properties • The first thing you must decide is what properties your object will have: • Let’s say we have a class called Name • Maybe a Name object would have the properties of first and last names (both Strings) • There should be a variable for each property
class Name • public class Name { private String first, last; public Name( ) { first = “Joe”; last = “Schmoe”; } }
Overloading Constructors • Remember that methods can be overloaded as long as the parameters are different. • The same is true for Constructors • The last example was an empty constructor, but we can allow objects to be constructed with the properties specified.
Overloaded class Name • public class Name { private String first, last; public Name( ) {first = “Joe”; last = “Schmoe”; } public Name(String theFirst, String theLast) { first = theFirst; last = theLast; } }
Non-Static Methods for Name • public String getFirst( ) { return first; } • public String getLast( ) { return last; } • public String getWholeName( ) { return first + “ ” + last; }
Non-Statics cont. • public void setFirst(String aName ) { first = aName; } • public void setLast(String aName ) { last = aName; } • public void setWholeName(String aName1,String aName2) { first = aName 1; last = aName2; }
Mutator Methods • Mutator methods change a property of the object which they operate upon. • These are very commonly “set” methods, as they assign properties to objects. • setFirst( ) of the Name class and showMessageDialog( ) are good examples. • Mutator methods are almost always void type
Accessor Methods • Accessor methods return information about objects without altering them. • These are very commonly “get” methods as they obtain information about objects. • getFirst( ) of the Name class and length( ) of the String class are good examples. • It is common for accessors to have no parameters
Calls the constructor Mutator method Using Objects • Class objects are created in other classes. (Usually one with a main method.) • You have done this with the Random class: Random rand = new Random( ); rand.nextInt( );
Using a Name Object • Name ballPlayer = new Name( ); Name president = new Name(“Teddy”, “Roosevelt” ); • ballPlayer.setWholeName(“Ricky”, “Henderson”); • System.out.println(president.getFirst( )); System.out.println(ballPlayer.getLast( )); Displays: Teddy Displays: Henderson
Variable Specifiers • It is important that you make the class instance variables(object properties) private. • Private means that no other class has access to them. • Imagine you had a Bank account object had an int instance variable called balance: If you made the variable public, any other class could modify your balance!!!
Specifiers cont. • The keyword final makes a variable constant. • This means that it is not a variable- it cannot change values. • Because of this, it is O.K. make public final instance variables… Since they cannot be changed by their own class, no other class can change them either.
Method Access • Methods should generally be declared public since you want other classes to be able to use them. • If a method is only intended to be used by it’s own class, then it can be private.