1 / 17

Non-Static Classes

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( );

aviv
Download Presentation

Non-Static Classes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Non-Static Classes What is the Object of this lecture?

  2. 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( );

  3. 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.

  4. Constructor Syntax • public class Name { public Name( ) { … code…. } }

  5. 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

  6. class Name • public class Name { private String first, last; public Name( ) { first = “Joe”; last = “Schmoe”; } }

  7. 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.

  8. 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; } }

  9. Non-Static Methods for Name • public String getFirst( ) { return first; } • public String getLast( ) { return last; } • public String getWholeName( ) { return first + “ ” + last; }

  10. 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; }

  11. 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

  12. 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

  13. 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( );

  14. 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

  15. 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!!!

  16. 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.

  17. 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.

More Related