Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 106 Introduction to Computer Science I 03 / 26 / 2008 Instructor: Michael Eckmann.

Similar presentations


Presentation on theme: "CS 106 Introduction to Computer Science I 03 / 26 / 2008 Instructor: Michael Eckmann."— Presentation transcript:

1 CS 106 Introduction to Computer Science I 03 / 26 / 2008 Instructor: Michael Eckmann

2 Michael Eckmann - Skidmore College - CS 106 - Spring 2008 Today’s Topics Comments and/or Questions? Object oriented programming –static Create another program using object oriented topics

3 Static variables Because static variables represent classwide information, we don't need any objects to be instantiated if we want to use them. Instead, we use the class name and dot operator to access any public static members (variables or methods.)‏

4 static variables static variables are accessible in other classes if the static variables are declared public. They are accessible by using the class name followed by the dot operator and then the static variable name. e.g. if there was a public static variable named time_object_count in the Time class, we could access it in another class by referring to: Time.time_object_count If it was declared as private, then we would need to have a public static method in Time to provide “read” access to it. e.g. a method like: public static int getCount( )‏ { return time_object_count; }

5 static methods we could call this method in the following way: int current_count; current_count = Time.getCount( ); Notice that we used the class name (not an object name) before the dot operator to reference the static members of a class. We have seen this before, such as when we called a static method of class Integer: Integer.parseInt( some_str )‏ Note however that if we have instantiated objects of class Time, we would be able to access the static members either by the class name or the object name.

6 static methods so, assuming t1 is an object of type Time, after it is instantiated, we would be able to call a static method getCount( ) either by: Time.getCount( )‏ or t1.getCount( )‏ It is best (for readability purposes) however to use the class name way. That is, Time.getCount( ) is preferred because someone reading your code will know that getCount ( ) is a static method of class Time, which may be valuable in understanding the code.

7 static methods The methods in class Math that we’ve used are static methods. Recall that we don’t create an object of type Math. Instead, we call the methods in Math with the class name (Math) followed by the dot operator, followed by the method name. This is how static methods are called (with the class name, not an object name.)‏

8 Concepts recap Public vs. private instance variables Public vs. private methods Static vs. instance variables Static vs. non-static methods

9 Exercise Let's create a brand new class using all these concepts. Let's create a class to represent a Movie – Let's assume we want to store for each movie: Title Director Running time Year of release – Let's also assume we want to store a count of all the movies we have instantiated Let's also then create a program for inventory of a video/dvd rental store.


Download ppt "CS 106 Introduction to Computer Science I 03 / 26 / 2008 Instructor: Michael Eckmann."

Similar presentations


Ads by Google