1 / 29

COMP 110 Static variables and methods

COMP 110 Static variables and methods. Luv Kohli October 29, 2008 MWF 2-2:50 pm Sitterson 014. Announcements. Program 3 due Friday, October 31, 2pm. Questions?. Today in COMP 110. Some notes about documentation Static variables and methods. Instance variables and methods.

amie
Download Presentation

COMP 110 Static variables and methods

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COMP 110Static variables and methods Luv Kohli October 29, 2008 MWF 2-2:50 pm Sitterson 014

  2. Announcements • Program 3 due Friday, October 31, 2pm

  3. Questions?

  4. Today in COMP 110 • Some notes about documentation • Static variables and methods

  5. Instance variables and methods • Instance variables privateint age; private String name; • Methods publicint getAge() { return age; } • Calling methods on objects Student std = new Student(); std.setAge(20); System.out.println(std.getAge());

  6. static • static variables and methods belong to a class as a whole, not to an individual object • Where have we seen static before? • When would you want a method that does not need an object to be called?

  7. What about a pow method? // Returns x raised to the yth power, where y >= 0. publicintpow(int x, int y) { int result = 1; for (inti = 0; i < y; i++) { result *= x; } return result; } Do we need an object to call this method?

  8. static, some examples • static constants and variables • private static final int FACE_DIAMETER = 200; • public staticfinalint FEET_PER_YARD = 3; • privatestaticint numberOfInvocations; • static methods • publicstaticvoid main(String[] args) • publicstaticint pow(int x, int y)

  9. static version of pow method publicclassMathUtilities { // Returns x raised to the yth power, where y >= 0. publicstatic intpow(int x, int y) { int result = 1; for (inti = 0; i < y; i++) { result *= x; } return result; } } static keyword

  10. static • Static variables and methods can be accessed using the class name itself: • DimensionConverter.FEET_PER_YARD • int z = MathUtilities.pow(2, 4);

  11. Will this code compile? public class SomeClass { publicstaticfinaldouble PI = 3.14159; privateboolean sunny = true; publicstaticdouble area(double radius) { sunny = false; return PI * (radius * radius); } } • Code will not compile • static methods are invoked without an object • no access to instance variables or non-static methods ERROR!

  12. Will this code compile? public class SomeClass { publicstaticfinaldouble PI = 3.14159; public int data = 12; private void printData() { System.out.println(data); } publicstaticdouble area(double radius) { printData(); return PI * (radius * radius); } } ERROR!

  13. Will this code compile? public class SomeClass { publicstaticfinaldouble PI = 3.14159; private void printPi() { System.out.println(PI); System.out.println(area(3.0)); } publicstaticdouble area(double radius) { return PI * (radius * radius); } } • Nonstatic methods CAN call static methods and access static variables

  14. Calling a nonstatic method from a static method public class SomeClass { publicstaticfinaldouble PI = 3.14159; private void printPi() { System.out.println(PI); System.out.println(area(3.0)); } publicstaticdouble area(double radius) { SomeClass sc = new SomeClass(); sc.printPi(); return PI * (radius * radius); } }

  15. Self-test questions • Can you call a nonstatic method from a static method? • Can you call a static method from a nonstatic method? • Can you access an instance variable inside a static method?

  16. main is a static method import java.util.*; publicclass MyClass { publicstaticvoid main(String[] args) { System.out.println(“Give me a number, and I will ” + “tell you its square and its square’s square.”); Scanner kb = new Scanner(System.in); int num = kb.nextInt(); int numSquared = num * num; System.out.println(“The square is ” + numSquared); int numSquaredSquared = numSquared * numSquared; System.out.println(“The square’s square is ” + numSquaredSquared); } }

  17. static helper methods import java.util.*; publicclass MyClass { publicstaticint square(int x) { return x * x; } publicstaticvoid main(String[] args) { System.out.println(“Give me a number, and I will ” + “tell you its square and its square’s square.”); Scanner kb = new Scanner(System.in); int num = kb.nextInt(); int numSquared = square(num); System.out.println(“The square is ” + numSquared); int numSquaredSquared = square(numSquared); System.out.println(“The square’s square is ” + numSquaredSquared); } }

  18. The Math class • Provides many standard mathematical methods, all static • do not create an instance of the Math class to use its methods • Call Math class’ methods using class name • Math.abs • Math.max • Math.min • Math.pow • Math.round • Others • Predefined constants • Math.PI • Math.E

  19. Using Math’s methods and constants publicstaticdouble largeToSmallthPower(int a, int b) { double small = Math.min(a, b); double large = Math.max(a, b); return Math.pow(large, small); } public static double area(double radius) { return Math.PI * (radius * radius); }

  20. Math.round • Math.round: returns closest long (or int, if using a float) to argument • Math.round(2.3) • Returns 2 • Math.round(2.7) • Returns 3

  21. Math.floor • Math.floor: returns largest double value less than or equal to argument and equal to a mathematical integer • Math.floor(2.3) • Returns 2.0 • Math.floor(2.7) • Returns 2.0

  22. Math.ceil • Math.ceil: returns smallest double value greater than or equal to argument and equal to a mathematical integer • Math.ceil(2.3) • Returns 3.0 • Math.ceil(2.7) • Returns 3.0

  23. What if you want an int? • Math.ceil returns a double • Math.ceil(5.6) returns 6.0 int num = (int) Math.ceil(5.6);

  24. Wrapper classes • All primitive types have an associated wrapper class • We have seen some of these before (where?): • Byte • Short • Integer • Long • Float • Double • Character • Boolean

  25. Wrapper classes int num = Integer.parseInt(“36”); • Integer.parseInt, Double.parseDouble, etc. are all static methods • These wrapper classes also have nonstatic methods

  26. Wrapper classes • You can create an instance of a wrapper class and use the instance to convert the value to different types. • Example: • Integer num = new Integer(36); • double numAsDouble = num.doubleValue();

  27. Wrapper classes • Useful static constants and methods • Examples: • Integer.MAX_VALUE • Double.MIN_VALUE • Float.parseFloat(“23.7”); • Long.toString(368);

  28. class Character • Character.toUpperCase • Character.toLowerCase • Character.isUpperCase • Character.isLowerCase • Character.isWhiteSpace • Character.isLetter • Character.isDigit

  29. Friday • Program 3 due, 2pm • Lab practice

More Related