The different kinds of variables in a Java program.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
Local Variables and Scope Benjamin Fein. Variable Scope A variable’s scope consists of all code blocks in which it is visible. A variable is considered.
The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been.
Variable types We have already encountered the idea of a variable type. It is also possible to think about variables in terms of their kind, namely: 1)
Summary of the lecture We discussed –variable scope –instance variable declarations –variable lifetime.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Road Map Introduction to object oriented programming. Classes
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Miscellaneous topicsCS-2301 B-term Miscellaneous Topics CS-2301, System Programming for Non-majors (Slides include materials from The C Programming.
C Lecture Notes Functions (Cont...). C Lecture Notes 5.8Calling Functions: Call by Value and Call by Reference Used when invoking functions Call by value.
Supplementary for method, DCO10803, Quarter 3, Page 1 of 7 Object-Oriented Programming and Design DCO10803 Supplementary for method  Prototype.
Understanding class definitions Looking inside classes.
Cs164 Prof. Bodik, Fall Symbol Tables and Static Checks Lecture 14.
Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.
The different types of variables in a Java program.
COMP More About Classes Yi Hong May 22, 2015.
Java Programming, Second Edition Chapter Four Advanced Object Concepts.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Memory organization - storing variables efficiently in the RAM memory.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Local Variables Garbage collection. © 2006 Pearson EducationParameters2 of 10 Using Accessors and Mutators Together What if you want to get a property.
The scope of local variables. Murphy's Law The famous Murphy's Law says: Anything that can possibly go wrong, does. (Wikipedia page on Murphy's Law:
18. DECLARATIONS.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Introduction to programming in the Java programming language.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
Lecture 13: Working with Multiple Programmers. Headers Header files: Each standard library has a corresponding header. The function prototype for all.
 2007 Pearson Education, Inc. All rights reserved Random Number Generation  rand function – Load – Returns "random" number between
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Introduction to Methods. Previously discussed There are similarities in make up of that can help you remember the construct of a class a class in the.
Dale Roberts CSCI 230 Functions Scope, Parameter Passing, Storage Specifiers Department of Computer and Information Science, School of Science, IUPUI Dale.
The life time of local variables (in a method). Local variables Local variable: A local variable is used to store information that is relevant for the.
Classes: user-defined types. Organizing method with a class A class is used to organize methods * Methods that compute Mathematical functions * The Scanner.
KIC/Computer Programming & Problem Solving 1.  Header Files  Storage Classes  Scope Rules  Recursion Outline KIC/Computer Programming & Problem Solving.
SEEM3460 Tutorial Java Keyword – static. static Variables class XY { static int x; // class variable int y; // instance variable } XY xy1 = new XY();
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science) MTech[IT],MPhil (Comp.Sci), MCA, MSc[IT], PGDCA, ADCA, Dc. Sc. & Engg.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
Chapter 6 Methods Chapter 6 - Methods.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
+ Storage Classes and Linkage. + Introduction Scope describe the region or regions of a program that can access and identifier Variables can be shared.
MIT-AITI: Functions Defining and Invoking Functions Functions as Data Function Scope: The call Object Function Arguments: The arguments objects Function.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Test 2 Review Outline.
C Functions -Continue…-.
Modern JavaScript Develop And Design
Programming Fundamentals Lecture #7 Functions
Using local variable without initialization is an error.
Scope, Parameter Passing, Storage Specifiers
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
ECE 103 Engineering Programming Chapter 12 More C Statements
Welcome back to Software Development!
Local variables and how to recognize them
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Topics to cover Instance variables vs. local variables.
Methods Scope How are names handled?
Storage Classes.
Scope Rules.
Presentation transcript:

The different kinds of variables in a Java program

Java has 4 different kinds of variables Class variables Instance variables Local variables Parameter variables The kind of variable is determined by: Where the variable is defined Whether the keyword static was used in the variable definition.

Properties of variables (1) Every variable has 2 properties: life time = the duration that a variable exists scope = the region in the program where the variable is accessible (can be used)

Properties of variables (2) To accommodate different needs: Short term information: Some information need only be retained within a (one) method(We call this kind of information local information) Long term information: Some information must be retained across several methods

Local variables (1) A local variable is used to store information that is relevant for the duration of the execution of one method A local variable will exists as long as the method in which they have been created is still running As soon as the method terminates (i.e., returns), all the local variables defined inside the method are destroyed.

Local variables (2) A local variable is defined inside the body of a method

Local variables (3)

Local variables (4) A local variable only exists between: The definition of the local variable The end of the method where the local variable is defined

Scope of a variable Scope of a variable = the region in the program where the variable is accessible Between the 2 location above, the scope of a variable is further limited by the block in which it is defined

The scope of local variables (1)

The scope of local variables (2) The access of the variable r was limited to the block in which it was defined !!!

The scope of local variables (3)

The scope of local variables (4) You cannot define different variables with the same name inside the same scope

Non-overlapping (or disjoint) scopes (1) Disjoint scopes = 2 or more scopes that do not overlap with each other

Non-overlapping (or disjoint) scopes (2)

Scoping rule for nested scope (1)

Scoping rule for nested scope (2)

Parameter Variables

Parameter variables (1) A parameter variable is used to store information that is being passed from the location of the method call into the method that is called

Parameter variables (2)

Parameter variables (3)

Defining parameter variables (1) A parameter variable is defined inside the brackets (... ) of the header of a method

Defining parameter variables (2)

The life time and scope of parameter variables (1) The life time of a parameter variable is the entire body of the method A parameter variable behaves like a local variable that is defined at the start of the method

The life time and scope of parameter variables (2) You cannot define different variables with the same name inside an outer scope and inside an inner scope In Java, you cannot define a local variable inside a method that has the same name as a parameter variable.

Class Variables

Class Variables (1) A class variable is used to store long term information that are used by many different methods in the program Can have the public access Must use the keyword static Inside the braces {... } of a class outside the braces {... } of every method

Class Variables (2)

Accessing a class variable (1) A class variable is accessed using its complete name: nameOfClass.nameOfClassVariable

Accessing a class variable (2) We can reference a class variable defined inside the same class without using the class name

The life time and scope of (public) class variables (1) Class variables exist for the entire execution of the Java program Class variables are accessible in every method of every class

The life time and scope of (public) class variables (2) When the method has a local variable or a parameter variable is equal to the name of the class variable, then: The class variable with the same name can no longer accessible with the short hand notation in that scope !!!

The life time and scope of (public) class variables (3)