Static Poisoning - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

Static Poisoning

Description:

... an individual object you can use it only by 'sending a message' to that object ... You cannot send a message to yourself, or use any instance variables this is a ... – PowerPoint PPT presentation

Number of Views:15
Avg rating:3.0/5.0
Slides: 12
Provided by: davidleem
Category:

less

Transcript and Presenter's Notes

Title: Static Poisoning


1
Static Poisoning
2
Review class and instance variables
  • int is a data type3 is a value (or instance) of
    that type
  • A class is a data typean 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 cant refer to an instance variable if you
    dont have an instance
  • You always have the class
  • The keyword static marks a variable as a class
    variable

3
Review class and instance methods
  • An instance method belongs to an individual
    objectyou 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)) ...

4
Static context
  • An application requires a public static void
    main(String args) method
  • 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 myTextField.setText("Hello")
  • You cannot send a message to yourself, or use any
    instance variablesthis is a static context, not
    an object
  • non-static variable xxx cannot be referenced from
    a static context

5
About the term
  • Static poisoning refers the fact that, in an
    application, you cant 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 useI made it up
  • There is a simple solution to this problem

6
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
static
static
static
7
Another example
public class Narcissus int x int y
int z public static void main(String
args) x 5 y 10
z x y
8
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

9
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

10
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

11
The End
Write a Comment
User Comments (0)
About PowerShow.com