public class Foo { private int var; public int Method1() { // var accessible anywhere here } public int MethodN() {

Similar documents
Lecture 5: Methods CS2301

Chapter 4: Writing Classes

ECE 122. Engineering Problem Solving with Java

Anatomy of a Class Encapsulation Anatomy of a Method

Chapter 1 Getting Started

class objects instances Fields Constructors Methods static

Announcements. PS 3 is due Thursday, 10/6. Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am

Object Class. EX: LightSwitch Class. Basic Class Concepts: Parts. CS257 Computer Science II Kevin Sahr, PhD. Lecture 5: Writing Object Classes

1.00 Lecture 8. Using An Existing Class, cont.

CS-140 Fall 2017 Test 1 Version Practice Practie for Sept. 27, Name:

CMPE-013/L. Introduction to C Programming. Unit testing

CS125 : Introduction to Computer Science. Lecture Notes #6 Compound Statements, Scope, and Advanced Conditionals

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Introduction to Programming Using Java (98-388)

CHAPTER 7 OBJECTS AND CLASSES

CHAPTER 7 OBJECTS AND CLASSES

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

CS110D: PROGRAMMING LANGUAGE I

Lexical Considerations

Static Methods. Why use methods?

Pace University. Fundamental Concepts of CS121 1

Last Time. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

1 Lexical Considerations

CS313D: ADVANCED PROGRAMMING LANGUAGE

CSE 142 Su 04 Computer Programming 1 - Java. Objects

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

For example, when we first started Java programming we described the HelloWorld class:

Java Methods. Lecture 8 COP 3252 Summer May 23, 2017

Lexical Considerations

The University of Melbourne Department of Computer Science and Software Engineering Software Design Semester 2, 2003

Loops. CSE 114, Computer Science 1 Stony Brook University

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year

Java: introduction to object-oriented features

A Fast Review of C Essentials Part II

6.096 Introduction to C++

CS-201 Introduction to Programming with Java

COE318 Lecture Notes Week 3 (Week of Sept 17, 2012)

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods

Function Call Stack and Activation Records

Functions and Recursion

COE318 Lecture Notes Week 4 (Sept 26, 2011)

JAVA: A Primer. By: Amrita Rajagopal

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

Names, Scopes, and Bindings. CSE 307 Principles of Programming Languages Stony Brook University

CS-140 Fall 2017 Test 1 Version Practice Practie for Sept. 27, Name:

CSCI312 Principles of Programming Languages!

A Short Summary of Javali

Why Pointers. Pointers. Pointer Declaration. Two Pointer Operators. What Are Pointers? Memory address POINTERVariable Contents ...

Introduction to Java

Encapsulation. Administrative Stuff. September 12, Writing Classes. Quick review of last lecture. Classes. Classes and Objects

Lecture 05: Methods. AITI Nigeria Summer 2012 University of Lagos.

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

M e t h o d s a n d P a r a m e t e r s

ITI Introduction to Computing II

Outline. Overview. Control statements. Classes and methods. history and advantage how to: program, compile and execute 8 data types 3 types of errors

CS/ENGRD 2110 SPRING Lecture 5: Local vars; Inside-out rule; constructors

Today's Topics. CISC 458 Winter J.R. Cordy

Data Structures using OOP C++ Lecture 3

Relationship of class to object

Java: Classes. An instance of a class is an object based on the class. Creation of an instance from a class is called instantiation.

News and information! Review: Java Programs! Feedback after Lecture 2! Dead-lines for the first two lab assignment have been posted.!

CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II

Names, Scopes, and Bindings. CSE 307 Principles of Programming Languages Stony Brook University

CMSC 331 Second Midterm Exam

Software Design and Analysis for Engineers

Chapter 6 Introduction to Defining Classes

Values and Variables 1 / 30

( &% class MyClass { }

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

Fundamentals of Programming Session 12

Class, Variable, Constructor, Object, Method Questions

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

Programming with Java

Lecture 16: Static Semantics Overview 1

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

(Not Quite) Minijava

Object-Oriented Programming Concepts

Basics of Java Programming

Java Identifiers, Data Types & Variables

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

2/3/2018 CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II. Lecture Contents. C# basics. Methods Arrays. Dr. Amal Khalifa, Spr17

CPS122 Lecture: Defining a Class

Lecture Set 4: More About Methods and More About Operators

Table of Contents Date(s) Title/Topic Page #s. Chapter 4: Writing Classes 4.1 Objects Revisited

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

More on Functions. Lecture 7 COP 3014 Spring February 11, 2018

Introduction to Java. Handout-1d. cs402 - Spring

CS Internet programming Unit- I Part - A 1 Define Java. 2. What is a Class? 3. What is an Object? 4. What is an Instance?

STRUCTURING OF PROGRAM

Lecture 18 Tao Wang 1

Array. Array Declaration:

Opening Problem. Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.

Encapsulation. You can take one of two views of an object: internal - the structure of its data, the algorithms used by its methods

Introduction to Programming (Java) 4/12

Object Oriented Modeling

Objects and Classes: Working with the State and Behavior of Objects

Programming Language (2) Lecture (4) Supervisor Ebtsam AbdelHakam Department of Computer Science Najran University

Transcription:

Scoping, Static Variables, Overloading, Packages In this lecture, we will examine in more detail the notion of scope for variables. We ve already indicated that variables only exist within the block they are defined. This is because they are allocated on the stack and are popped off when the block exits. If we want to pass return values from a method, we need to supply a return value or pass variables by reference (through a class). This lecture will explain in more detail about where variables may be accessed depending on where they are defined. Scoping In the last lecture, we said that local variables are those declared within a block or those passed as value parameters. The block does not have to be the body of a method local identifiers can be declared within any block. In a large program, there may be several variables with the same identifier name, for example. How do we know which variable is meant and where each variable is accessible? The answers to these questions are provided by scope rules. Java uses a type of scoping called static scoping. It is called static because the scope of identifiers is determined at compile time. In contrast, some languages (like Lisp) use dynamic scope, where the scope is determined at runtime. There are two categories of scope for an identifier in Java that are generally used: class scope, and local scope. Both adhere to the same basic rule: a variable is accessible everywhere within the set of curly braces where it is declared, including code within nested curly braces. When a variable is declared within the class, it is accessible from all other methods in that class. These are either class or instance variables (we ll describe the difference in a moment): public class Foo private int var; public int Method1() // var accessible anywhere here public int MethodN() // var accessible anywhere here public class Foo2 var defined in class Foo not accessible here

Note that variables defined in the class give us a means to share information among different methods in the class. Any value stored in these variables is lost when the object is destroyed (e.g., a variable referencing it goes away). We can also control access to these variables with the public and private modifiers. We do not have the public and private modifiers when a variable is declared within a method. These are variables with local scope; they are accessible only within the braces which they are defined, and disappear when method exits. This means that variables defined in a method are not directly accessible outside that method. This includes variables passed as parameters: public class Foo public int Method1(int x1) int x2; // only x1 and x2 accessible here // destroyed when Method1 exits public int MethodN(String s1, String s2) // only s1 and s2 accessible here // destroyed when MethodN exits public class Foo2 // No variables from Foo s // methods accessible here In normal usage, variable scoping is as simple as defined above. However, things get trickier when variables have the same name. For example, consider the following scenario: public class Foo private int x; public int Method1() int x; x = 10; // Which x is changed?

In this example we have two variables named x. One has class scope, the other has local scope. Which variable is referenced when there is this ambiguity? The rule used in Java is that the scope of a variable begins with its most recent declaration. This means that local variables take precedence over class variables. In the example above, the local variable is changed while the class variable remains unchanged. Here is an example: public class OutputTest public public void DoIt() // Local variable a a=3; System.out.println(a); // Outputs 3 // Sub-block a = 5; // Same local variable a System.out.println(a); // Outputs 5 return; // Local "a" destroyed public static void main(string[] args) OutputTest x = new OutputTest(); x.a = 100; x.doit(); System.out.println(x.a); // Outputs 100 The output of this program is 3 5 100 Inside main, we only have access to class variable a. Inside DoIt, we reference the local variable a while the class variable remains unchanged.

Java does not allow us to have multiple local variables with the same name within the same scope. The following is illegal; public void DoIt() // Local variable a a=3; // Sub-block // NOT ALLOWED a = 5; // Same local variable a However, Java does allow us to have multiple sub-blocks with the same local variable name. Each local variable is unique only to its block: public class OutputTest public public void DoIt() int i=0; // Sub-block a = 1; System.out.println(a); // "a" destroyed a = 2; System.out.println(a); while (i<1) // More typical formation of sub-block int a=3; System.out.println(a); i++; public static void main(string[] args) OutputTest x = new OutputTest(); x.doit();

The output of this program is: 1 2 3 Each variable a is defined and exists only within its sub-block. This means the variable a is created and destroyed three times in this example. The keyword this From these examples, it may seem like it is impossible to access a variable defined in the class if it has the same name as a local variable. This is true if we just use the variable name. However, Java provides a keyword called this that refers to the current object. From this we can access class variables. For example: public class OutputTest public public void DoIt() // Local variable a a=3; System.out.println(a); // Outputs 3 this.a = 50; // Changes class instance variable return; // Local "a" destroyed public static void main(string[] args) OutputTest x = new OutputTest(); x.a = 100; x.doit(); System.out.println(x.a); // Outputs 50 This program outputs: 3 50 The class variable is changed from the DoIt method when we preface the variable with this rather than the local variable. If class identifiers are accessible to all methods, why don't we just make all identifiers global and do away with parameter lists? Good programming practice dictates that communication between the modules of our program should be explicitly stated. This practice limits the possibility of one module accidentally interfering with another. In

other words, each method is given only what it needs to know. This is how we write "good" programs. Consider a large project where many programmers are working on different sections of the program. If everyone used class variables, each programmer would have to be extremely careful not to pick a variable name that another programmer was using, or the program might not compile or may produce buggy output. The existence of local variables avoids this problem by allowing each programmer to define and have access to their own private set of variables that will never conflict with someone else s code. Static Variables Previously we discussed static methods. Static methods are defined and exist within the class definition, or blueprint. This means that there is one and only one method, and this means that we don t have to create an instance of the class to reference that method. We can do the same thing with variables. If we add the keyword static in front of a variable defined in the class, then this becomes a static class variable. There is only one copy of this variable that is shared among all instances of the class. Static variables are useful when we need to share the same information among all instances of a class. A method that is defined as static only has access to static class variables. Here is an example that illustrates the use of static variables. We would like a counter to keep track of how many times instances of class Foo have been created. Each time the class is created, in the constructor we increment the static counter. Each individual instance has access to this counter as well as an individual ID number: public class OutputTest private static int counter = 0; // # of instances created private int num; // Which number am I? // Constructor keeps track of number of times created public OutputTest() counter++; // Increment global static variable num = counter; // Set local instance to global value // Print out our information public void PrintInfo() System.out.println("I am instance " + num + " of " + counter);

public static void main(string[] args) OutputTest i1 = new OutputTest(); i1.printinfo(); OutputTest i2 = new OutputTest(); OutputTest i3 = new OutputTest(); i3.printinfo(); OutputTest i4 = new OutputTest(); i3.printinfo(); i4.printinfo(); The output from this program is: I am instance 1 of 1 I am instance 3 of 3 I am instance 3 of 4 I am instance 4 of 4 Each time we create a new instance, the static counter is increased by one. information is accessible to each class. This Method Overloading Let s say that we want to write a method, AbsoluteValue, that can operate on integers, floats, and doubles and return the absolute value of the number that is passed in. Using what we know so far we would have to write three separate functions: public class Absolutes public static int AbsoluteValueInt(int x) public static float AbsoluteValueFloat(float x) public static double AbsoluteValueDouble(double x) From main, we could invoke these methods as:

public static int main(string[] args) int x=-3; float f=(float) 34.5; double d=-3.812312; System.out.println(Absolutes.AbsoluteValueInt(x)); System.out.println(Absolutes.AbsoluteValueFloat(f)); System.out.println(Absolutes.AbsoluteValueDouble(d)); We would have to write a separate function for each data type, and give each function a different name to distinguish one from the other. Fortunately, Java supports a feature called method overloading that lets us assign the same name to methods that have different parameter types. The compiler is able to distinguish which function we want to use by matching up the data types in the parameters. For example we could write: public static int AbsoluteValue(int x) public static float AbsoluteValue(float x) public static double AbsoluteValue(double x) public static int main(string[] args) int x=-3; float f=(float) 34.5; double d=-3.812312; System.out.println(Absolutes.AbsoluteValue(x)); System.out.println(Absolutes.AbsoluteValue(f)); System.out.println(Absolutes.AbsoluteValue(d)); In this case, we have named all of the functions the same thing, AbsoluteValue. The compiler is able to invoke the proper function call by matching up the parameters. You might notice that all of the AbsoluteValue methods are basically the same except the data type. In a future class we ll discuss templates that allow us to write the method only once.

As long as there is a unique set of parameters, the compiler can make the distinction. For example, the following would not be valid: public static float AbsoluteValue(float x) public static double AbsoluteValue(float x) In this case, even though both methods return different data types, they both expect the same parameters. Consequently, the compiler won t be able to distinguish one from the other, resulting in a compile-time error. Note that overloading also works if we don t have the same number of parameters. The following functions can be overloaded successfully based on the different number (as opposed to type) of parameters: int foo(int x) return 3; int foo(int x, int y) return 4; A function making the invocation of either foo(x) or foo(x,y) will successfully invoke the proper method. Packages and Importing A package is a collection of classes that have been grouped together and given a name. Generally the classes are all related in some way (e.g. utilities, math function, etc.) To give a file a package name, each file should have as the first line: package name_of_package; The name of the package is generally all lowercase letters. For example: package mystuff.coolcode;

To use the classes in a package elsewhere, use the import statement at the top of the file (which we ve already been using for various predefined Java classes): import mystuff.coolcode.*; The above would give us access to all classes in the package mystuff.coolcode. wanted to be more specific we could identify specific classes: If we import mystuff.coolcode.animation; import mystuff.coolcode.funstuff; The package name is not an arbitrary identifier, but tells the compiler where to find the classes on the disk. Each. Corresponds to an actual directory: mystuff.lib.math Would be located in directory mystuff/lib/math. These directories are relative to the current working directory or to the CLASSPATH directory, which you can set as an environment variable (e.g. to c:\jdk\lib) NetBeans lets you create packages quite easily, and by default will ask you to put your classes into packages.