{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

14_static_packages

14_static_packages - Static methods Example Java's built in...

Info icon This preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
4/27/13 1 Static revisited Static methods // Example: // Java's built in Math class public class Math { public static int abs(int a) { if (a >= 0) { return a; } else { return -a; } } public static double toDegrees(double radians) { return radians * 180 / PI; } } // Using the class: System.out.println(Math.abs(-5)); //didn’t need to create any object Static methods n static : Part of a class, not part of an object. n Static methods: q Do not require an instance of the class and do not understand the implicit parameter , this ; therefore, cannot access an object's instance variables q good for code related to a class but not to each object's state q if public , can be called from inside or outside the class Static variables n static : Part of a class, rather than part of an object. q Classes can have static variables . q Static variables are not replicated in each object; a single variable is shared by all objects of that class. private static type name ; or, private static type name = value ; q Example: private static int count = 0; Example n You are writing a class to represent a bank account, and you would like the constructor to automatically assign a running number as the account number. n How can static variables help you? Assigning ids for BankAccount public class BankAccount { // static variable for assigning an account number // (shared among all instances of the class) private static int lastAssignedNumber = 1000;
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern