Static is one of the modifiers that determine variable and method characteristics. The static modifier associates a variable or method with its class.

Slides:



Advertisements
Similar presentations
Copyright © 2012 Pearson Education, Inc. Chapter 14: More About Classes.
Advertisements

EC-241 Object-Oriented Programming
CSCI 1100/ , 6.2, 6.4 April 12, 15, 17.
Lecture 6 b Last time: array declaration and instantiationarray declaration and instantiation array referencearray reference bounds checkingbounds checking.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Enhancing classes Visibility modifiers and encapsulation revisited
ECE122 L16: Class Relationships April 3, 2007 ECE 122 Engineering Problem Solving with Java Lecture 16 Class Relationships.
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
What is a class? a class definition is a blueprint to build objects its like you use the blueprint for a house to build many houses in the same way you.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Reference … and Misc Other Topics Clark Savage Turner, J.D., Ph.D. Some lecture slides have been adapted from those developed.
Static Class Members Wrapper Classes Autoboxing Unboxing.
Static members Based on Java tutorial by Oracle: svars.html
INF 523Q Chapter 5: Enhancing Classes. 2 b We can now explore various aspects of classes and objects in more detail b Chapter 5 focuses on: object references.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Like our natural language. Designed to facilitate the expression and communication ideas between people and computer Like our natural language. Designed.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley John Lewis, Peter DePasquale, and Joseph Chase Chapter 5: Writing Classes.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
6-1 Object-Oriented Design Today we focuses on: –the this reference (Chapter 7) –the static modifier (Chapter 7) –method overloading (Chapter 7)
CSE 1302 Lecture 7 Object Oriented Programming Review Richard Gesick.
Chapter 5: Enhancing Classes
Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
CSCI-383 Object-Oriented Programming & Design Lecture 14.
Chapter 7 Object-Oriented Design Concepts
Java Software Solutions Lewis and Loftus Chapter 4 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects and Classes -- Introduction.
Chapter 6 Object-Oriented Design. © 2004 Pearson Addison-Wesley. All rights reserved6-2 Object-Oriented Design Now we can extend our discussion of the.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
Object Oriented Programming (OOP) Lecture No. 11.
CSC241 Object-Oriented Programming (OOP) Lecture No. 6.
Static class members.
1 Enhancing Classes  Now we can explore various aspects of classes and objects in more detail  Chapter 5 focuses on: object references and aliases passing.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
CS170 ygao JAVA, C4Slide 1. CS170 ygao JAVA, C4Slide 2.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
Advanced Arithmetic, Conditionals, and Loops INFSY 535.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
© 2004 Pearson Addison-Wesley. All rights reserved October 31, 2007 Static Class Members ComS 207: Programming I (in Java) Iowa State University, FALL.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
This In Java, the keyword this allows an object to refer to itself. Or, in other words, this refers to the current object – the object whose method or.
Class Fundamentals BCIS 3680 Enterprise Programming.
Object-Oriented Design Chapter 7 1. Objectives You will be able to Use the this reference in a Java program. Use the static modifier for member variables.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
© 2004 Pearson Addison-Wesley. All rights reserved November 2, 2007 Class Relationships ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Chapter 5: Enhancing Classes
Methods Chapter 6.
Section 11.1 Class Variables and Methods
More Object Oriented Programming
Object Oriented Programming
METHODS AND BEHAVIORS AKEEL AHMED.
Unit-1 Introduction to Java
Chapter 5: Enhancing Classes
CSE 1030: Implementing GUI Mark Shtern.
Classes and Objects.
Static Class Members March 29, 2006 ComS 207: Programming I (in Java)
CS100J Lecture 8 Previous Lecture This Lecture Programming Concepts
Classes and Objects Part 2 Static Class Members and Arrays of Objects
Object Oriented Programming Using C++
Objects and Classes Creating Objects and Object Reference Variables
Object Oriented Programming Review
Object Oriented Programming in java
Submitted By : Veenu Saini Lecturer (IT)
Templates Generic Programming.
(C) 2010 Pearson Education, Inc. All rights reserved.
Object Oriented Programming (OOP) Lecture No. 12
Presentation transcript:

Static is one of the modifiers that determine variable and method characteristics. The static modifier associates a variable or method with its class rather than with an object. There are times when a class wants to have a variable that is shared by all the objects that comes from the class. If any one of the objects changes the value of this variable, the change is reflected in the state of every object from the class. The variable is declared and assigned a value as though it were an instance variable; however it uses the keyword static in its declaration. Static variables are also called class variables and like all instance variable, they are declared private. The static Modifier

The static Modifier Static methods (also called class methods) can be invoked through the class name rather than through a particular object For example, the methods of the Math class are static: Math.sqrt (25) To write a static method, we apply the static modifier to the method definition The static modifier can be applied to variables as well It associates a variable or method with the class rather than with an object Read after math example: We don’t have to instantiate an object to call a static method as we see in the Math.sqrt example.

Static Variables Static variables are also called class variables Normally, each object has its own data space, but if a variable is declared as static, only one copy of the variable exists private static float price; Memory space for a static variable is created when the class in which it is declared is loaded All objects created from the class share static variables The most common use of static variables is for constants

Static Methods class Helper public static int triple (int num) { int result; result = num * 3; return result; } class Helper Because it is static, the method can be invoked as: value = Helper.triple (5);

Static Methods The order of the modifiers can be interchanged, but by convention visibility modifiers come first Recall that the main method is static; it is invoked by the system without creating an object Static methods cannot reference instance variables, because instance variables don't exist until an object exists However, a static method can reference static variables or local variables Static methods and static variables often work together