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)

Slides:



Advertisements
Similar presentations
Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
Advertisements

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.
2.4 Creating and Using Objects. Writing the code for classes of your own will come later. At this time it is possible to understand and correctly write.
Static Methods Static methods are those methods that are not called on objects. In other words, they don’t have an implicit parameter. Random number generation.
Classes  All code in a Java program is part of a class  A class has two purposes  Provide functions to do work for the programmer  Represent data.
 It is possible to declare names for object references and not assign object references to them.  Such names literally refer to nothing at all.  It.
Road Map Introduction to object oriented programming. Classes
Recitation 09/12/2007 CS 180 Department of Computer Science, Purdue University.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Week 4 Recap CSE 115 Spring Formal Parameter Lists Comma-separated list of formal parameters Formal parameters are listed giving the type of the.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
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)
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
Constructors A constructor is a method that creates an object in Java. It does not return anything and it is not void. It’s only purpose is to create an.
The different kinds of variables in a Java program.
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
Java Unit 9: Arrays Declaring and Processing Arrays.
Java Programming, Second Edition Chapter Four Advanced Object Concepts.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Object Oriented Programming Elhanan Borenstein Lecture #4.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
4.1 Instance Variables, Constructors, and Methods.
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.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
Initialization & Cleanup Guaranteed initialization with the constructor The name of the constructor is the same as the name of the class.
1 Object-Oriented Programming (Java), Unit 12 Kirk Scott.
CPS120: Introduction to Computer Science Decision Making in Programs.
ECE122 Feb. 22, Any question on Vehicle sample code?
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Chapter 3 (B) 3.5 – 3.7.  Variables declared in a function definition’s body are known as local variables and can be used only from the line of their.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Classes Modeling the Object. Objects model the world Classes are programmer defined types that model the parts of a system Class serve as blueprints for.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
CLASS AND OBJECTS Valerie Chu, Ph.D. LeMoyne-Owen College 1.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Basic Object-Oriented concepts. Concept: Classes describe objects Every object belongs to (is an instance of) a class An object may have fields –The class.
1 Review for Midterm 2 Aaron Bloomfield CS 101-E.
Topics Instance variables, set and get methods Encapsulation
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Lecture 5: Some more Java!
Java Primer 1: Types, Classes and Operators
Chapter 3: Using Methods, Classes, and Objects
Object-Oriented Programming & Design Lecture 14 Martin van Bommel
Java Unit 11: Inheritance I
Defining Classes and Methods
Overloading and Overriding
The Three Attributes of an Identifier
SPL – PS3 C++ Classes.
Chapter 5 Classes.
Presentation transcript:

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) Local variables 2) Instance variables 3) Formal parameter variables 4) Static variables

Variable types, cont. Java only has these four kinds of variables. We have encountered all four kinds. Any variable that might be declared between a pair of opening and closing braces is local to that set of braces. The term is usually used for general purpose variables in programs. It is also possible to declare general purpose variables as needed in the bodies of methods. These are then local to that method.

Accessibility without declarations We have seen declarations of the accessibility of variables, including public, private, and no declaration at all. Local program variables are simply declared with their type, and they are accessible within the set of braces where they are defined. Parameters also do not have a public or private declaration. They are local to the method they belong to.

Accessibility with declarations Public and private declarations can be applied to instance and static variables (as well as methods). You may have noticed by accident that it is possible to declare instance variables with neither the keyword public or private. What kind of accessibility such variables have will be discussed in a future unit. In the meantime you should just use the convention that instance variables should be private and declare them that way.

Accessibility for public and private variables In addition to the question of whether a variable is declared public or private, for each kind of variable it is possible to answer the questions of when and where it comes into and goes out of active existence, where it is accessible within a program or a class, and whether or not it has default initialization. Here are the answers to these questions for the four kinds of variables.

Accessibility for local variables 1. Local variables come into existence at the point in code where they are declared. Their existence continues in time and space to the ending brace of the block they are declared in. As long as that block of code is executing they are in existence and can be used in that block. As noted in the section on loops, it is possible to have more than one variable with the same name literally within the same block because the blocks themselves are nested. The system distinguishes these in the following way: In the inner nesting the variable name refers to the variable declared there.

Accessibility for local variables, cont. Outside of that block, both before and after the inner block, the variable refers to the variable declared outside. Local variables do not have default initialization. It is usually a good idea for the programmer to initialize them in a program. The compiler will tend to remind you to do so.

Accessibility for instance variables 2. Instance variables come into existence when the object they belong to is constructed. They go out of existence when that object no longer exists. For our purposes they no longer exist when we no longer have a handle on the object anywhere in our program. If declared private, as they should be, they are only accessible from within the class in which they are declared.

Accessibility for instance variables, cont. In other words, anywhere in the constructors or methods of that class it is possible to use the instance variable without qualification and without using a get method. When instance variables are used without qualification the object they belong to is the one being constructed or the implicit parameter of the code they appear in. From outside of their class code, in a program for example, instance variables can only be accessed by the use of a method.

Accessibility for instance variables, cont. Instance variables do have default initialization. Numeric variables are initialized to 0, boolean variables to false, and object references to null. The programmer is always free to override these initializations in constructors, or write constructors which explicitly do such initializations so that there is no doubt what values instance variables have when reading the code.

Accessibility for formal parameters 3. Formal parameters come into existence when the call to a method or constructor is made. They go out of existence when the return is made from such code. They are accessible anywhere inside the block of code belonging to the method or constructor. Even though method code is shared, because the variables go out of existence at the end of the call, the values in one call cannot be confused with the values in the next call. Formal parameters are initialized to whatever actual parameter values are passed to them at the outset of a call.

Accessibility for static variables 4. Static variables come into existence when a program importing or using their class begins to run. They go out of existence when the program ends. Static variables may be either public or private, depending on their purpose. If public, they can be freely accessed from anywhere in a program which imported the class by using dot notation. In other words, they can be referred to by ClassName.VariableName

Accessibility for static variables, cont. If they are declared private, then they are freely accessible from any point within the constructor or method code of the class and can only be accessed from outside of the class code with a suitable method. They have the same default initialization as instance variables, which can be overridden by the programmer.

Simplest Constructor Now that we know that instance variables have default initialization, it is possible to flesh out our knowledge of what kinds of constructors are possible. For any class it is possible to write a constructor which does not take any parameters and which does not contain any code. For example: public MyCup6() { }

Simplest Constructor, cont. This is the simplest way of writing a constructor where every instance variable would be initialized to the default value. It is easy to write—the disadvantage is that when looking at the code you do not get a reminder of what those instance variables are or what the default values are for different variable types. It is necessary to remember.

Default Constructor supplied It is also possible to write the code for a class without any constructors at all. If this is done, the system will provide a default constructor. No code for this will be visible, but it will be possible to make the usual call to a constructor with the same name as the class and provide no parameters. An instance will be created with all instance variables initialized to default values.

Default Constructor not supplied As usual, it’s also possible to write constructors that take parameters. If you write the code for even one constructor in your own class, the default constructor will not be provided by the system. If you want a default constructor in that case, you’ll have to write it yourself.