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.

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

Introduction to Programming with Java, for Beginners dynamic vs. static static variables and methods.
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.
CIT 590 Intro to Programming Java lecture 3. Hashmaps The equivalent of python dictionaries. With both ArrayLists and Hashmaps, the syntax only allows.
1. 2 Introduction to Methods  Type of Variables  Static variables  Static & Instance Methods  The toString  equals methods  Memory Model  Parameter.
Starting Classes and Methods. Objects have behaviors In old style programming, you had: –data, which was completely passive –functions, which could manipulate.
19-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
20-Jun-15 Methods. Complexity The programmer's biggest adversary is complexity The programmer's biggest adversary is complexity Primitive types and the.
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.
25-Jun-15 Starting Classes and Methods. Objects have behaviors In old style programming, you had: data, which was completely passive functions, which.
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.
Unit 081 Introduction to Nested Classes Nested classes are classes defined within other classes The class that includes the nested class is called the.
10-Aug-15 Classes and Objects in Java. 2 Classes and Objects A Java program consists of one or more classes A class is an abstract description of objects.
Options for User Input Options for getting information from the user –Write event-driven code Con: requires a significant amount of new code to set-up.
Saravanan.G.
Applications in Java Towson University *Ref:
Classes and Objects. Topics The Class Definition Declaring Instance Member Variables Writing Instance Member Methods Creating Objects Sending Messages.
Static Keyword. What is static The static keyword is used when a member variable of a class has to be shared between all the instances of the class. All.
Programming using C# for Teachers Introduction to Objects Reference Types Functions of Classes Attributes and Types to a class LECTURE 2.
Questions? Suggestions?. References References Revisited What happens when we say: int x; double y; char c; ???
ECE122 Feb. 22, Any question on Vehicle sample code?
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
SE-1010 Dr. Mark L. Hornick 1 Java Programming Basics.
1 CS 177 Week 11 Recitation Slides Class Design/Custom Classes.
Java Programming static keyword.
ICBT  Basura Ramanayaka  Eshani werapitiya  Hasitha Dananjaya.
Operator Overloading Week 5.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
24-Dec-15 Class Structure. 2 Classes A class describes a set of objects The objects are called instances of the class A class describes: Fields (instance.
Static?. Static Not dynamic class Widget { static int s; int d; // dynamic // or instance // variable }
Recitation 8 User Defined Classes Part 2. Class vs. Instance methods Compare the Math and String class methods that we have used: – Math.pow(2,3); – str.charAt(4);
1 Object-Oriented Software Engineering CS Java OO Fundamentals Contents Classes and Objects Making new objects Method declarations Encapsulating.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
CIT 590 Intro to Programming Lecture 13. Some Eclipse shortcuts CTRL + SHIFT + F – format file (proper indentation etc). Please do this before you submit.
Java Programming Final Keyword In Java. final keyword The final keyword in java is used to restrict the user. The final keyword can be used in many context.
Chapter 5 : Methods Part 2. Returning a Value from a Method  Data can be passed into a method by way of the parameter variables. Data may also be returned.
Constructor OverloadingtMyn1 Constructor Overloading One context in which you will regularly need to use overloading is when you write constructors for.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 12.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
Review – Primitive Data What is primitive data? What are 3 examples of primitive data types? How is a piece of primitive data different from an object?
CLASSES IN JAVA Primitive Types Example of a Class Allocating Objects of a Class Protecting Class data Constructors Static data and Static Methods.
Staples are our staple Building upon our solution.
A Simple Object Oriented Program public class Simple { public static void main (String [] args) { System.out.println(“howdy”); } System.out is an object.
Static Members Static Variables & Methods SoftUni Team Technical Trainers Software University
AKA the birth, life, and death of variables.
Class Structure 15-Jun-18.
Some Eclipse shortcuts
Classes and Objects in Java
Namespaces, Scopes, Access privileges
Using local variable without initialization is an error.
Class Structure 16-Nov-18.
CSC 113 Tutorial QUIZ I.
Class Structure 28-Nov-18.
Variables and Their scope
Class Structure 7-Dec-18.
Assignment 7 User Defined Classes Part 2
Class Structure 2-Jan-19.
Classes and Objects in Java
Sampath Kumar S Assistant Professor, SECE
AKA the birth, life, and death of variables.
class PrintOnetoTen { public static void main(String args[]) {
Class Structure 25-Feb-19.
CLASSES, AND OBJECTS A FIRST LOOK
Java’s Central Casting
Classes and Objects in Java
Classes and Methods 15-Aug-19.
Presentation transcript:

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 a value (instance) of that type A class variable belongs to the class as a whole; there is only one of it An instance variable belongs to individual objects; there is one of it for each object, but none for the class as a whole You can’t refer to an instance variable if you don’t have an instance You “always” have the class The keyword static marks a variable as a class variable

3 Example of a static variable class Person { String name; // Instance variable boolean alive; // Instance variable static int population; // Class variable Person(String name) { // Constructor this.name = name; alive = true; population++; } public void die() // Method alive = false; population--; }

4 Review: class and instance methods An instance method “belongs to” an individual object—you can use it only by “sending a message” to that object Example: String s = myTextField.getText(); Example: saddamHussein.die(); A class (static) method belongs to a class Examples: –y = Math.abs(x); –if (Character.isLetter(ch)) {... } –population = (int)(0.9 * population); // war

5 Static context An application requires a public static void main(String args[]) method It must be static because, before your program starts, there aren’t 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: myTextField.setText("Hello"); –You cannot send a message to yourself, or use any instance variables—this is a static context, not an object non-static variable xxx cannot be referenced from a static context

6 About the term Static poisoning refers the fact that, in an application, you can’t access non-static variables or methods from a static context, so you end up making more and more things static “Static poisoning” is not a term that is in widespread use—I made it up There is a simple solution to this problem

7 An example of static poisoning public class StaticPoison { int x; int y; public static void main(String args[]) { doOneThing(); } void doOneThing() { x = 5; doAnotherThing(); } void doAnotherThing() { y = 10; } static error

8 Another 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 A solution public class JustAdd { int x; int y; int z; public static void main(String args[]) { JustAdd myself = new JustAdd(); myself.x = 5; myself.y = 10; myself.z = myself.x + myself.y; } }

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

11 The best solution Know when a variable or method should be 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) When you are writing a “main” class with a main method: –Would it possibly make sense to have more than one of this “main” object? –If so, create one in your main method and use it

12 Summary In an application, frequently the best way to write the main method is as follows: class MyClass { public static void main(String args[]) { MyClass myself = new MyClass(); myself.doAllTheWork(); } void doAllTheWork() { // Note: not static } }

13 The End