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.

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.
Introduction to C# Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
20-Jun-15 Methods. Complexity The programmer's biggest adversary is complexity The programmer's biggest adversary is complexity Primitive types and the.
1 What’s a class People are confused with nested classes and anonymous classes. Experiments in a recitation last week revealed that the problems were probably.
Classes and Objects in Java
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.
26-Jun-15 Methods. About methods A method is a named group of declarations and statements You execute those declarations and statements by calling the.
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.
Unit 081 Introduction to Nested Classes Nested classes are classes defined within other classes The class that includes the nested class is called the.
 To be able to write larger programs ◦ By breaking them down into smaller parts and passing data between the parts.  To understand the concepts of Methods.
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.
1 BUILDING JAVA PROGRAMS CHAPTER 3 THE SCANNER CLASS AND USER INPUT.
Saravanan.G.
Applications in Java Towson University *Ref:
Introduction to Objects A way to create our own types.
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.
Interfaces. –An interface describes a set of methods: no constructors no instance variables –The interface must be implemented by some class. 646 java.
The scope of local variables. Murphy's Law The famous Murphy's Law says: Anything that can possibly go wrong, does. (Wikipedia page on Murphy's Law:
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) public static void main(String[]
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.
CPRG 215 Introduction to Object-Oriented Programming with Java Module 3- Introduction to Object Oriented Programming concepts Topic 3.6 Access Modifiers,
Initialization & Cleanup Dr. Ramzi Saifan Slides adapted from Prof. Steven Roehrig.
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
Lecture 10:Static & Final Kenya © 2005 MIT-Africa Internet Technology Initiative MyMath Example public class MyMath { public double PI = ;
Lab 01-2 Objectives:  Writing a Java program.  How to send output to the command line console.  Learn about escape sequences.  Learn how to compile,
Java Programming static keyword.
ICBT  Basura Ramanayaka  Eshani werapitiya  Hasitha Dananjaya.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
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 }
Java and C++ Transitioning. A simple example public class HelloWorldApp { public static void main(String[] args) { //Display the string. System.out.println("Hello.
MIT AITI 2004 Lecture 10 Static and Final. public class MyMath { public double PI = ; public double square (double x) { return x * x; } public.
1 Object-Oriented Software Engineering CS Java OO Fundamentals Contents Classes and Objects Making new objects Method declarations Encapsulating.
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.
CS116 COMPILER ERRORS George Koutsogiannakis 1. How to work with compiler Errors The Compiler provide error messages to help you debug your code. The.
Constructor OverloadingtMyn1 Constructor Overloading One context in which you will regularly need to use overloading is when you write constructors for.
OOP Basics Classes & Methods (c) IDMS/SQL News
Introduction to programming in java Lecture 22 Arrays – Part 2 and Assignment No. 3.
CSC 212 – Data Structures Lecture 6: Static and non-static members.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
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.
Static Members Static Variables & Methods SoftUni Team Technical Trainers Software University
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.
AKA the birth, life, and death of variables.
using System; namespace Demo01 { class Program
Classes and Objects in Java
Class Structure 16-Nov-18.
Java: Getting Started Basic Class Structure
Class Structure 7-Dec-18.
Assignment 7 User Defined Classes Part 2
Class Structure 2-Jan-19.
S.VIGNESH Assistant Professor, SECE
Static in Classes CSCE 121 J. Michael Moore.
JAVA Constructors.
Classes and Objects in Java
Sampath Kumar S Assistant Professor, SECE
AKA the birth, life, and death of variables.
References and Objects
class PrintOnetoTen { public static void main(String args[]) {
Class Structure 25-Feb-19.
CLASSES, AND OBJECTS A FIRST LOOK
Overview of Java 6-Apr-19.
Classes and Objects in Java
Presentation transcript:

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 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

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(); A class (static) method belongs to a class Examples: –y = Math.abs(x); –if (Character.isLetter(ch)) {... }

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

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

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

Another example public class Narcissus { int x; int y; int z; public static void main(String args[]) { x = 5; y = 10; z = x + y; }

An attempted solution public class Narcissus { int x; int y; int z; public static void main(String args[]) { Narcissus myself = new Narcissus(); myself.x = 5; myself.y = 10; myself.z = myself.x + myself.y; } }

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

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 } }

The End