TAILIEUCHUNG - Lecture Introduction to programming with Java - Chapter 9: Classes with class members

Chapter 9 - Classes with class members. This chapter presents the following content: Class Variables, class methods, how to access class members, when to use class members, class constants, example program using class members. | Chapter 9 - Classes with Class Members Class Variables Class Methods How to Access Class Members When to Use Class Members Class Constants Example Program Using Class Members 1 Class Variables Based on what you've learned so far, when you're working on an object-oriented program, you should envision separate objects, each with their own set of data and behaviors (instance variables and instance methods, respectively). That's a valid picture, but you should be aware that in addition to data and behaviors that are specific to individual objects, you can also have data and behaviors that relate to an entire class. Since they relate to an entire class, such data and behaviors are referred to as class variables and class methods, respectively. For a particular class, each of the class's objects has its own copy of the class's instance variables. For a particular class, each of the class's objects shares a single copy of the class's class variables. For a mouse growth simulation program, name some appropriate instance variables and some appropriate class variables. 2 Class Variables If you'd like a variable to be shared by all the objects within the variable's class, make it a class variable by using the static modifier in its declaration: static ; Example: public class Mouse { private static int mouseCount; private static double averageLifeSpan; . Class variables are declared at the top of the class, above all the methods. 3 Class Variables Class variables use the same default values as instance variables: integer types get 0 floating point types get boolean types get false reference types get null What are the default values for the class variables in this code fragment? public class Mouse { private static int mouseCount; private static double averageLifeSpan; private static String researcher; private static int simulationDuration = 730; . Initializations are allowed. 4 Scope You can access a class .

TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.