- The Java Workshop
- David Cuartielles Andreas G?ransson Eric Foster Johnson
- 688字
- 2021-06-11 13:05:21
Working with Objects in Java
Objects are to classes what variables are to data types. While classes define the structure and possible actions of a certain data type, objects are actual usable parts of the computer memory containing that data. The action of creating an object is known as making an instance of a class. In a sense, it is like making a copy of the template and then modifying it by accessing its variables or methods. Let's see this in action:
Computer myPC = new Computer( 2.5 );
myPC is the actual object. We would say that myPC is an object of the class Computer in colloquial terms.
The different fields and methods inside the class can be accessed by typing the name of the object followed by a period and the name of the variable or method you want to address. Making any changes to the variables or calling the methods will take effect only within the scope of that object. If you had more objects of the same class in your program, each one of them would have a piece of memory of its own. An example of how to address a method is as follows:
myPC.setCpuSpeed( 2.5 );
An example of how to address a variable, on the other hand, would be the following assignment:
myPC.cpuSpeed = 2.5;
Because of the way the Computer class has been defined, the last two code listings have the exact same effect. The whole class has been defined—by default—as public, which means that all the methods, variables, and objects from the class are available to be called with the mechanism described previously. It could be necessary to prevent users from directly interacting with the variables within the class and only allow their modification through certain methods.
The different components within a class can be defined as public or private. The former will make the component available to be used as shown so far, while the latter will hinder the ability of other developers to access that part. The following example shows how to make the cpuSpeed variable private:
Example02.java
1 class Computer {
2 // variables
3 private double cpuSpeed; // in GHz
4
5 // constructor
6 Computer() {
7 cpuSpeed = 0;
8 }
9
10 // methods
11 void setCpuSpeed ( double _cpuSpeed ) {
12 cpuSpeed = _cpuSpeed;
13 }
14
15 double getCpuSpeed () {
16 return cpuSpeed;
17 }
18 }
The result of this code listing is the same as before:
2.5
Process finished with exit code 0
If you tried to access the cpuSpeed variable directly from the Example02 class, the program would throw an exception. The following example shows such a case. Try it out to see how the debugger informs you when you try to access a private variable:
Example03.java
20 public class Example03 {
21 public static void main(String[] args) {
22 Computer myPC = new Computer();
23 myPC.setCpuSpeed( 2.5 );
24 System.out.println( myPC.cpuSpeed );
25 }
26 }
The result of this program is:
Example03.java:23: error: cpuSpeed has private access in Computer
System.out.println( myPC.cpuSpeed );
1 error
Process finished with exit code 1.
What the compiler shows is an error in the Computer class, which has been derived from java.lang.
- Learning Java Functional Programming
- 從零開始:數(shù)字圖像處理的編程基礎與應用
- The Modern C++ Challenge
- Mastering ServiceStack
- arc42 by Example
- 薛定宇教授大講堂(卷Ⅳ):MATLAB最優(yōu)化計算
- 實戰(zhàn)低代碼
- 軟件測試技術指南
- Working with Odoo
- 深入分布式緩存:從原理到實踐
- Multithreading in C# 5.0 Cookbook
- Windows Embedded CE 6.0程序設計實戰(zhàn)
- 深入解析Java編譯器:源碼剖析與實例詳解
- Visual C++從入門到精通(第2版)
- Building Apple Watch Projects