DOC PREVIEW
Penn CIT 591 - Static

This preview shows page 1-2-3-4 out of 12 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

staticReview: class and instance variablesReview: class and instance methodsExampleStatic contextAbout the termAn example of static poisoningAnother exampleAn attempted solutionThe best solutionSummaryThe EndstaticReview: 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 variableReview: 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)) { ... }Exampleclass Circle {static double PI = 3.1416;int x, y;double radius;}Circle circle1 = new Circle(...); Circle circle2 = new Circle(...);PI3.141650x50y5.0radius80x50y8.0radiusCirclecircle1circle2Static 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 contextAbout 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 problemAn example of static poisoningpublic class StaticPoison { int x; int y; public static void main(String args[]) { doOneThing(); } void doOneThing() { x = 5; doAnotherThing(); } void doAnotherThing() { y = 10; }}staticstaticstaticstaticAnother examplepublic 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


View Full Document

Penn CIT 591 - Static

Documents in this Course
Stacks

Stacks

11 pages

Arrays

Arrays

30 pages

Arrays

Arrays

29 pages

Applets

Applets

24 pages

Style

Style

33 pages

JUnit

JUnit

23 pages

Java

Java

32 pages

Access

Access

18 pages

Methods

Methods

29 pages

Arrays

Arrays

32 pages

Methods

Methods

9 pages

Methods

Methods

29 pages

Vectors

Vectors

14 pages

Eclipse

Eclipse

23 pages

Vectors

Vectors

14 pages

Recursion

Recursion

24 pages

Animation

Animation

18 pages

Animation

Animation

18 pages

Eclipse

Eclipse

23 pages

JAVA

JAVA

24 pages

Arrays

Arrays

29 pages

Animation

Animation

18 pages

Numbers

Numbers

21 pages

JUnit

JUnit

23 pages

Access

Access

18 pages

Applets

Applets

24 pages

Methods

Methods

30 pages

Buttons

Buttons

20 pages

Java

Java

31 pages

Style

Style

28 pages

Style

Style

28 pages

Load more
Download Static
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Static and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Static 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?