Presentation is loading. Please wait.

Presentation is loading. Please wait.

Tonga Institute of Higher Education

Similar presentations


Presentation on theme: "Tonga Institute of Higher Education"— Presentation transcript:

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

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

3 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

4 Variable Visibility and Brackets
Demonstration Variable Visibility and Brackets

5 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

6 Scope with Duplicate Variable Names
Demonstration Scope with Duplicate Variable Names

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

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

9 Customer and CustomerDriver
Demonstration Customer and CustomerDriver

10 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

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

12 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

13 Demonstration Item and Item Driver

14 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

15 Demonstration Item and Item Driver

16 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

17 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

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

19 PlaneTicket and PlanetTicketDriver
Demonstration PlaneTicket and PlanetTicketDriver


Download ppt "Tonga Institute of Higher Education"

Similar presentations


Ads by Google