Presentation is loading. Please wait.

Presentation is loading. Please wait.

University of Central Florida COP 3330 Object Oriented Programming

Similar presentations


Presentation on theme: "University of Central Florida COP 3330 Object Oriented Programming"— Presentation transcript:

1 University of Central Florida COP 3330 Object Oriented Programming

2 Agenda Variables

3 Variables

4 Variables Instance Variables (Non-Static Fields)
Objects store their individual states in non-static fields (i.e. fields declared without keyword static) Non-static fields are also known as instance variables because their values are unique to each instance of a class (to each object, in other words)

5 Variables Class Variables (Static Fields)
A class variable is any field declared with the static modifier Keyword static tells the compiler that there is exactly one copy of this variable in existence, regardless of how many times the class has been instantiated. Example: the field defining the number of gears for a particular kind of bicycle could be marked as static since conceptually the same number of gears will apply to all instances. static int numGears = 6;  the keyword final could be added to indicate that the number of gears will never change.

6 Variables Local Variables
Similar to how an object stores its state in fields, a method will often store its temporary state in local variables. The syntax for declaring a local variable is similar to declaring a field int count = 0; No special keyword designating a variable as local A local variable is based on the variable being declared between the opening and closing braces of a method. Local variables are only visible to the methods in which they are declared; Local variables are not accessible from the rest of the class

7 Variables Parameters The signature for the main method is
public static void main(String[] args) the args variable is the parameter to this method Parameters are always classified as "variables" not "fields". This applies to other parameter-accepting constructs as well Constructors Exception handlers

8 Variables Naming Variable names are case-sensitive.
Can be any legal identifier unlimited-length sequence of Unicode letters and digits beginning with a letter, the dollar sign "$", or the underscore character "_" The convention is to always begin your variable names with a letter, not "$" or "_". The dollar sign character, by convention, is never used at all The underscore character is technically legal, though is discouraged White space is not permitted

9 Variables Naming Subsequent characters may be
letters Digits dollar signs underscore characters Use full words instead of cryptic abbreviations Makes code easier to read and understand May make code self-documenting must not be a keyword or reserved word.

10 Keywords and Reserved Words
abstract continue for new switch assert*** default goto* package synchronized boolean do if private this break double implements protected throw byte else import public throws case enum**** instanceof return transient catch extends int short try char final interface static void class finally long strictfp** volatile const* float native super while

11 Variables Naming Single word variables Multi-word variables
spell that word in all lowercase letters Multi-word variables capitalize the first letter of each subsequent word (i.e. camel casing) Variables that store a constant value Capitalize every letter separate subsequent words with the underscore character. static final int NUM_GEARS = 6,


Download ppt "University of Central Florida COP 3330 Object Oriented Programming"

Similar presentations


Ads by Google