Tonga Institute of Higher Education

Slides:



Advertisements
Similar presentations
PHP functions What are Functions? A function structure:
Advertisements

Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
CSC1351 9/6/2011 Where are we?. Polymorphism // Use dynamic lookup to allow different data types to be manipulated // with a uniform interface. public.
Applying OO Concepts Using Java. In this class, we will cover: Defining classes Defining methods Defining variables Encapsulation Class methods and variables.
Road Map Introduction to object oriented programming. Classes
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
Writing methods and Java Statements. Java program import package; // comments and /* … */ and /** javadoc here */ public class Name { // instance variables.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Classes with multiple methods Part 1. Review of classes & objects Early on, we learned that objects are the basic working units in object-oriented programs.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Java Review CIS 304 Intermediate Java Programming for Business.
Java Unit 9: Arrays Declaring and Processing Arrays.
The Ruby Programming Language
Chapter 5 - Writing a Problem Domain Class Definition1 Chapter 5 Writing a Problem Domain Class Definition.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
CIT 590 Intro to Programming First lecture on Java.
10-Nov-15 Java Object Oriented Programming What is it?
Code Conventions Tonga Institute of Higher Education.
Copyright Curt Hill Variables What are they? Why do we need them?
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Working With Objects Tonga Institute of Higher Education.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis & Matt Bauer.
Object-Oriented Programming: Classes and Objects Chapter 1 1.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
INTRODUCTION TO CLASSES & OBJECTS CREATING CLASSES USING C#
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Abstract classes and interfaces
OOP: Encapsulation &Abstraction
Objects as a programming concept
Classes (Part 1) Lecture 3
3 Introduction to Classes and Objects.
Examples of Classes & Objects
Object-Oriented Programming: Classes and Objects
Objects as a programming concept
Chapter 3: Using Methods, Classes, and Objects
University of Central Florida COP 3330 Object Oriented Programming
Classes.
Object Oriented Systems Lecture 03 Method
Lecture 14 Writing Classes part 2 Richard Gesick.
Object-Oriented Programming: Classes and Objects
Java LESSON 7 Objects, Part 1
Can perform actions and provide communication
Abstract classes and interfaces
Can perform actions and provide communication
Defining Classes and Methods
Variables ICS2O.
Tonga Institute of Higher Education
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Encapsulation and Constructors
Unit-1 Introduction to Java
Chapter 9 Objects and Classes
Object Oriented Programming in java
Can perform actions and provide communication
Tonga Institute of Higher Education
Applying OO Concepts Using Java
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Abstract classes and interfaces
Defining Classes and Methods
In this class, we will cover:
Object Oriented Programming
Chap 2. Identifiers, Keywords, and Types
ITM 352 Functions.
Presentation transcript:

Tonga Institute of Higher Education Object Variables Tonga Institute of Higher Education

What is a Scope? The scope of a variable defines what parts of a program can see the variable or method

Scope: Brackets Brackets can change visibility of variables A variable is valid from when it is declared to where the braces end. This variable is not seen because it was defined inside of another pair of brackets

Variable Visibility and Brackets Demonstration Variable Visibility and Brackets

Scope: Global Variables vs. Local Variables Variables defined outside of a method but inside of a class Local Variables Variables defined inside of brackets They are only visible by code inside the brackets If a variable name is already being used globally, you can still declare it locally. When looking for a value, start local. Go global if you can’t find the variable

Scope with Duplicate Variable Names Demonstration Scope with Duplicate Variable Names

How to Define a Variable public static String firstName Access Specifier Special Keyword Name Type

Access Specifiers Access Specifier Name public static String firstName Special Keyword Name Type Default – This is usable by: Code in the same class Code in classes in the same package Public – This can be used by everything: Code in classes in different packages Private – This can only be used by: Code inside the same class Protected – This can be used by: Note: This does not include inheritance visibility. Therefore, Default and Protected look like they are the same.

Customer and CustomerDriver Demonstration Customer and CustomerDriver

Special Keywords This is optional final static abstract public static String firstName Access Specifier Special Keyword Name Type This is optional final For Constants - Variables with values that will never change Code Convention: Use all capitals and separate words with underscores Ex: QUESTION_MESSAGE, ERROR_MESSAGE static Covered in next slide abstract Will be covered later in Object Inheritance lecture

Static Keyword There is only one copy of a static variable. All instances and the class use the same value If the variable is defined outside of a method but inside the class, it is a class variable. This variable is accessible in 2 ways: <instance name>.<class variable> <class name>.<class variable> Built-in Java classes will often have static methods if an object is not required… like calculating square root.

Class Variables, Instance Variables and Local Variables Global Variables Variables defined outside of a method but inside of a class Instance Variable Each instance uses it’s own value Access these with dot notation <instance name>.<instance variable> Class Variables Static keyword is used All instances and the class use the same value Every member that a static method uses must also be static Access these with dot notation from an instance <instance name>.<class variable> You can also access variables directly from the class name <class name>.<class variable> Local Variables Variables defined inside of a method They are only visible by code inside the same method

Demonstration Item and Item Driver

Variable Type and Name Access Specifier Name public static String firstName Access Specifier Special Keyword Name Type The type can be a primitive or an object Example: int, boolean, String, Integer The name should be: One word The first letter is lower case. The first letter of each internal word is capitalized. Keep your variable names simple and descriptive. Example: String firstName int phoneNumber

Demonstration Item and Item Driver

Code Conventions Generally, make all variables private Use accessors to access your variables Accessors – methods used to access a class’ variables Get method – a method used to get the value of a variable Set method – a method used to set the value of a variable

Accessors: Get Methods Used to retrieve variable values from a class No parameter Return value A good place to call methods that calculate return values

Accessors: Set Methods Used to set values for variables Require a parameter No return value A good place to call validation methods

PlaneTicket and PlanetTicketDriver Demonstration PlaneTicket and PlanetTicketDriver