Introduction to Programming with Java, for Beginners dynamic vs. static static variables and methods.

Slides:



Advertisements
Similar presentations
Introduction to classes Sangeetha Parthasarathy 06/11/2001.
Advertisements

UNIT II DATA TYPES OPERATORS AND CONTROL STATEMENT 1.
1 Classes and Objects in Java Basics of Classes in Java.
7 Copyright © 2005, Oracle. All rights reserved. Creating Classes and Objects.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 3: Flow Control I: For Loops.
1 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t)
The Line Class Suppose you are involved in the development of a large mathematical application, and this application needs an object to represent a Line.
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Lecture 15.1 Static Methods and Variables. © 2006 Pearson Addison-Wesley. All rights reserved Static Methods In Java it is possible to declare.
Introduction to C# Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
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.
Introduction to Programming with Java, for Beginners Intro OOP with Java Java Program Structure.
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk.
Static Poisoning. Review: class and instance variables int is a data type; 3 is a value (or instance) of that type A class is a data type; an object is.
26-Jun-15 Methods. About methods A method is a named group of declarations and statements If a method is in the same class, you execute those declarations.
Classes with multiple methods Part 1. Review of classes & objects Early on, we learned that objects are the basic working units in object-oriented programs.
Using Statics. 2 Review: class and instance variables int is a data type; 3 is a value (or instance) of that type A class is a data type; an object is.
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.
Like our natural language. Designed to facilitate the expression and communication ideas between people and computer Like our natural language. Designed.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "methods" in Java Purpose Reuse code Modularize the program This.
Lecture # 5 Methods and Classes. What is a Method 2 A method is a set of code which is referred to by name and can be called (invoked) at any point in.
Objective You will be able to define and identify the basic components of a java program by taking notes, seeing examples, and completing a lab. Construction.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Object Oriented Programming (OOP) Lecture No. 11.
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
Writing Static Methods Up until now, we have been USING (calling) static methods that other people have written. Now, we will start CREATING our own static.
Objects and Classes Mostafa Abdallah
Classes: user-defined types. Organizing method with a class A class is used to organize methods * Methods that compute Mathematical functions * The Scanner.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Java methods Methods break down large problems into smaller ones Your program may call the same method many times saves writing and maintaining same code.
ICBT  Basura Ramanayaka  Eshani werapitiya  Hasitha Dananjaya.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
Building java programs, chapter 3 Parameters, Methods and Objects.
Classes - Intermediate
CPSC 233 Tutorial 5 February 9 th /10 th, Java Classes Each Java class contains a set of instance variables and methods Instance Variables: Type.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
CS100A, Lect. 7, 22 Sept. 98. Static Fields and Methods 1 CS100A, Lecture 7, 22 Sept Static fields and methods. Suppose that we would like to main-
2-Oct-16 Basic Object-Oriented Concepts. 2 Concept: An object has behaviors In old style programming, you had: data, which was completely passive functions,
CPSC 233 Tutorial 12 March 4/5 th, TopHat Quiz int[] a = {0}; int[] b = {1}; a = b; What is the value of a[0] i) 0 ii) 1.
Topic: Classes and Objects
Chapter 7 User-Defined Methods.
Examples of Classes & Objects
AKA the birth, life, and death of variables.
Arrays 3/4 By Pius Nyaanga.
Static Members and Methods
Interface.
Methods Chapter 6.
CS100A, Lecture 7, 22 Sept Static fields and methods
Classes and Objects in Java
Classes and Objects in Java
Chapter 4 Procedural Methods.
CMSC 202 Static Methods.
Classes & Objects: Examples
Variables and Their scope
Java Lesson 36 Mr. Kalmes.
CS2011 Introduction to Programming I Methods (II)
AKA the birth, life, and death of variables.
Simple Classes in Java CSCI 392 Classes – Part 1.
Chapter 7 Procedural Methods.
Programs and Classes A program is made up from classes
Applying OO Concepts Using Java
Classes and Objects Static Methods
Dr. R Z Khan Handout-3 Classes
Arrays 3/4 June 3, 2019 ICS102: The course.
Chapter 6 Arrays.
Visibilities and Static-ness
Presentation transcript:

Introduction to Programming with Java, for Beginners dynamic vs. static static variables and methods

1 Dynamic Variables and Methods All instance variables and methods weve created so far have been dynamic Note: There is no dynamic keyword in Java Dynamic by default In general, dynamic refers to things created at run time i.e. when the program is running Every object gets its own (dynamic) instance variables. Every object effectively gets its own copy of each dynamic method The absence of the keyword static before non-local variables and methods means dynamic (one per object/instance)

2 Static Variables Static means pertaining to the class in general, not to an individual object A variable may be declared (outside of a method) with the static keyword: E.g. static int numTicketsSold; There is one variable numTickets for the class not one per object!!! A static variable is shared by all instances (if any). All instances may be able read/write it A static variable that is public may be accessed by: ClassName.variableName E.g. Math.PI

3 Static Methods A method may be declared with the static keyword Static methods live at class level, not at object level Static methods may access static variables and methods, but not dynamic ones how could it choose which one? public static int getNumSold(){ return numTicketsSold; }

4 Static Methods (contd..) A static method that is public can be accessed ClassName.methodName(args) double result = Math.sqrt(25.0); int numSold = Ticket.getNumberSold();

5 Example: Ticket public class Ticket{ private static int numTicketsSold = 0; // shared private int ticketNum; // one per object public Ticket(){ numTicketsSold++; ticketNum = numTicketsSold; } public static int getNumberSold() { return numTicketsSold; } public int getTicketNumber() { return ticketNum; } public String getInfo(){ return "ticket # " + ticketNum + "; " + numTicketsSold + " ticket(s) sold."; } }

6 Example: Ticket > Ticket.getNumberSold() 0 > Ticket t1 = new Ticket(); > t1.getTicketNum() 1 > t1.getInfo() "ticket # 1; 1 ticket(s) sold." > t1.getNumberSold() 1 > Ticket t2 = new Ticket(); > t2.getTicketNum() 2 > t2.getInfo() "ticket # 2; 2 ticket(s) sold." > t2.getInfo() "ticket # 2; 2 ticket(s) sold." > t1.getInfo() "ticket # 1; 2 ticket(s) sold." > Ticket.getNumberSold() 2

7 Static context To have standalone Java Application we need a public static void main(String args[]) method The main method belongs to the class in which it is written It must be static because, before your program starts, there arent any objects to send messages to This is a static context (a class method) You can send messages to objects, if you have some objects: d1.bark(); You cannot send a message to yourself, or use any instance variables - this is a static context, not an object Non-static variable cannot be referenced from a static context

8 Example public class JustAdd { int x; int y; int z; public static void main(String args[]) { x = 5; y = 10; z = x + y; } all are wrong

9 Solution public class JustAdd { int x; int y; int z; public static void main(String args[]) { JustAdd myAdd = new JustAdd() myAdd.doItAll() } void doItAll() { x = 5; y = 10; z = x + y; } }

10 When to use static A variable should be static if: It logically describes the class as a whole There should be only one copy of it A method should be static if: It does not use or affect the object that receives the message (it uses only its parameters)

11 Static Rules static variables and methods belong to the class in general, not to individual objects The absence of the keyword static before non- local variables and methods means dynamic (one per object/instance) A dynamic method can access all dynamic and static variables and methods in the same class A static method can not access a dynamic variable (How could it choose or which one?) A static method can not call a dynamic method (because it might access an instance variable)