using statics l.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Using Statics PowerPoint Presentation
Download Presentation
Using Statics

Loading in 2 Seconds...

play fullscreen
1 / 13

Using Statics - PowerPoint PPT Presentation


  • 114 Views
  • Uploaded on

Using Statics. 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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Using Statics' - zorion


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
review class and instance variables
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
example of a static variable
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--; }

review class and instance methods
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
static context
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
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

error

error

error

error

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

another example

all are wrong

Another example

public class JustAdd {

int x;

int y;

int z;

public static void main(String args[]) {

x = 5;

y = 10;

z = x + y;

}

}

a solution
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;
  • }}
a better solution
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;
  • }}
the best solution
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
summary
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 }

}