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”

Slides:



Advertisements
Similar presentations
Chapter 7: User-Defined Functions II
Advertisements

Lecture 2: Object Oriented Programming I
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
ECE122 L16: Class Relationships April 3, 2007 ECE 122 Engineering Problem Solving with Java Lecture 16 Class Relationships.
C++ Classes in Depth. Topics Designing Your Own Classes Attributes and Behaviors Writing Classes in C++ Creating and Using Objects.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Lecture 22 Miscellaneous Topics 4 + Memory Allocation.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Elements of a Java Program Bina Ramamurthy SUNY at Buffalo.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 6: Object-Oriented Programming.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
Methods. Methods are groups of statements placed together under a single name. All Java applications have a class which includes a main method class MyClass.
Introduction to Object-oriented Programming
Andrew(amwallis) Classes!
Topic: Classes and Objects
Classes and Objects Introduced
Concepts of Object Oriented Programming
Programming Logic and Design Seventh Edition
Chapter 7: User-Defined Functions II
University of Central Florida COP 3330 Object Oriented Programming
Classes and OOP.
Introduction to Computer Science / Procedural – 67130
Classes and Objects in Java
University of Central Florida COP 3330 Object Oriented Programming
Lecture 4 D&D Chapter 5 Methods including scope and overloading Date.
CompSci 230 Software Construction
Introduction to Objects
Java LESSON 7 Objects, Part 1
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Class Constructor Recall: we can give default values to instance variables of a class The “numberOfPlayers” variable from the “PlayerData” class before.
Defining Your Own Classes Part 1
More inheritance, Abstract Classes and Interfaces
The super Reference Constructors cannot be used in child classes, even though they have public visibility Yet we often want to use the parent's constructor.
Defining Classes and Methods
Variables ICS2O.
CS 302 Week 8 Jim Williams, PhD.
Variables and Their scope
Encapsulation and Constructors
Outline Writing Classes Copyright © 2012 Pearson Education, Inc.
Java Programming Arrays
Object Oriented Programming
Java Programming Function Introduction
Object Oriented Programming in java
Classes and Objects in Java
Object Oriented Programming Review
Session 2: Introduction to Object Oriented Programming
CLASSES, AND OBJECTS A FIRST LOOK
Overview of Java 6-Apr-19.
Defining Classes and Methods
Defining Classes and Methods
Classes, Objects and Methods
Andy Wang Object Oriented Programming in C++ COP 3330
Announcements Assignment 2 and Lab 4 due Wednesday.
Dr. R Z Khan Handout-3 Classes
Java Programming Function Introduction
Methods 16-May-19.
Classes and Objects in Java
Object-Oriented Programming and class Design
Chapter 7 Objects and Classes
Previous Lecture: Today’s Lecture: Reading (JV):
Object-Oriented Programming and class Design
Threads and concurrency / Safety
Introduction to Objects
Presentation transcript:

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

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! }

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

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

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

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

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

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

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

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?

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!

Object instances In memory, this is the situation:

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

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