Presentation is loading. Please wait.

Presentation is loading. Please wait.

Class Variables We’ve seen how to add extra subroutines into our programs Can be accessed from within one another We can also add variables that are “global”

Similar presentations


Presentation on theme: "Class Variables We’ve seen how to add extra subroutines into our programs Can be accessed from within one another We can also add variables that are “global”"— Presentation transcript:

1 Class Variables We’ve seen how to add extra subroutines into our programs Can be accessed from within one another We can also add variables that are “global” inside the class Defined outside functions, but inside the class Like a function, but with variable declaration Example: Class myClass{ public static String someName; private static int counter; private static double interest_rate; }

2 Class variables Be careful: the same way you cannot re-define a variable inside a function, you cannot have naming conflicts with global variables as well class myClass{ static int counter = 0; public static void main(String[] args){ int counter; // Error! }

3 Example: Guessing game
We can add two features to the standard Guessing game easily using subroutines and class variables Keep track of number of games played Keep track of number of games won Main idea: Wrap the “single game” logic in its own subroutine Use two class variable counters that increment at the appropriate time and are displayed after the user quits

4 Object-Oriented Design
What are programs for? Communication Data storage and access Entertainment (Most of) the uses for computers are based in the real-world E.g. “documents”, “file”, “mail” It makes sense to try and capture real-world concepts from within a program

5 Object-oriented design
Ultimately, modern programs are made by people People develop an understanding of the world through objects and their properties Different objects (e.g. a car) may have: Attributes (e.g. color, wheels, gears) Behavior (e.g. accelerate, decelerate, honk the horn) Objects may interact with each other E.g. Engine turns axle, which turns the wheels

6 Object-oriented design
Object-oriented programming is a slight departure from the standard “imperative” design we have been working with Designing a set of objects to model different aspects of a problem vs stripping them into bare data We’ve programmed a “game” but only superficially: we only used numbers and strings to simulate the playing of a game The main mechanism behind object-orientation in Java is the class

7 Classes Finally! WTF is a class????
In a way, a class is like a blueprint for a real-world object For each attribute (color, size, etc.) of the real-world object, we define a class variable For each behavior (accelerate, decelerate) we define a class method or function A class in Java actually occupies space in memory, similar to the variables we have been using all along Any functions or variables connected with the class are also given space in memory

8 Classes The following is a legal class definition: class UserData{
static String name; static int age; } When run, there is one copy of this in memory, with enough room to hold a string and an integer We can access the data like standard variables with the dot-operator: UserData.name UserData.age

9 Class instances Subtitle: what if we don’t make things static?
There is ever one copy of an actual class It holds all the static parts of the class But: what if we want multiple things, each with its own name an age? New definition: class PlayerData { static int playerCount; String name; int age; }

10 Class instances The PlayerData class will not have attributes name and age directly accessible: System.out.println(PlayerData.name); // Error System.out.println(PlayerData.age); // Error System.out.println(PlayerData.playerCount); // Works Only static members are accessible from the class name To access the non-static members?

11 Objects … we create an object of type PlayerData
Just like a regular variable! PlayerData johnny; But! This does not create the object yet PlayerData johnny = new PlayerData(); Then the non-static members are accessible through the object variable johnny.name = “johnny”; johnny.age = 21; johnny.playerCount = 10; // Error! Only on the class itself!

12 Object instances In memory, this is the situation:

13 Summary so far Static members: belong to the class
Non-static (aka dynamic) members: belong to object instances Access class members via the dot (“.”) operator: <ClassName>.<memberName> for static <instanceName>.<memberName> for dynamic

14 Classes in the big picture
A “Java Program” typically involves many interacting classes We have used several at the same time Scanner Math System Exactly one class may have a member function that fits the signature: public static void main(String[]) Any other functionality should happen as a side-effect of commands executed inside that main function


Download ppt "Class Variables We’ve seen how to add extra subroutines into our programs Can be accessed from within one another We can also add variables that are “global”"

Similar presentations


Ads by Google