|
| 1 | +The primary object-oriented construct in Java is the _class_, which is a combination of data ([_fields_][fields]), also known as instance variables, and behavior ([_methods_][methods]). The fields and methods of a class are known as its _members_. |
| 2 | + |
| 3 | +Access to members can be controlled through access modifiers, the two most common ones being: |
| 4 | + |
| 5 | +- [`public`][public]: the member can be accessed by any code (no restrictions). |
| 6 | +- [`private`][private]: the member can only be accessed by code in the same class. |
| 7 | + |
| 8 | +In Java if no access modifier is specified, the default is _package visibility_. In this case, the member is visible to all classes defined into the same package. |
| 9 | + |
| 10 | +The above-mentioned grouping of related data and behavior plus restricting access to members is known as [_encapsulation_][encapsulation], which is one of the core object-oriented concepts. |
| 11 | + |
| 12 | +You can think of a class as a template for creating instances of that class. To [create an instance of a class][creating-objects] (also known as an _object_), the [`new` keyword][new] is used: |
| 13 | + |
| 14 | +```java |
| 15 | +class Car { |
| 16 | +} |
| 17 | + |
| 18 | +// Create two car instances |
| 19 | +Car myCar = new Car(); |
| 20 | +Car yourCar = new Car(); |
| 21 | +``` |
| 22 | + |
| 23 | +[Fields][fields] have a type and a name (cased in [camelCase][camel-case]) and can be defined anywhere in a class (cased in [PascalCase][pascal-case]). |
| 24 | + |
| 25 | +```java |
| 26 | +class Car { |
| 27 | + // Accessible by anyone |
| 28 | + public int weight; |
| 29 | + |
| 30 | + // Only accessible by code in this class |
| 31 | + private String color; |
| 32 | +} |
| 33 | +``` |
| 34 | + |
| 35 | +One can optionally assign an initial value to a field. If a field does _not_ specify an initial value, it will be set to its type's [default value][default-values]. An instance's field values can be accessed and updated using dot-notation. |
| 36 | + |
| 37 | +```java |
| 38 | +class Car { |
| 39 | + // Will be set to specified value |
| 40 | + public int weight = 2500; |
| 41 | + |
| 42 | + // Will be set to default value (0) |
| 43 | + public int year; |
| 44 | +} |
| 45 | + |
| 46 | +Car newCar = new Car(); |
| 47 | +newCar.weight; // => 2500 |
| 48 | +newCar.year; // => 0 |
| 49 | + |
| 50 | +// Update value of the field |
| 51 | +newCar.year = 2018; |
| 52 | +``` |
| 53 | + |
| 54 | +Private fields are usually updated as a side effect of calling a method. Such methods usually don't return any value, in which case the return type should be [`void`][void]: |
| 55 | + |
| 56 | +```java |
| 57 | +class CarImporter { |
| 58 | + private int carsImported; |
| 59 | + |
| 60 | + public void ImportCars(int numberOfCars) |
| 61 | + { |
| 62 | + // Update private field |
| 63 | + carsImported = carsImported + numberOfCars; |
| 64 | + } |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +Note that is not customary to use public fields in Java classes. Private fields are typically used which are accessed through [_getters_ and _setters_][so-getters-setters]. |
| 69 | + |
| 70 | +Within a class, the [`this` keyword][this] will refer to the current class. This is especially useful if a parameter has the same name as a field: |
| 71 | + |
| 72 | +```java |
| 73 | +class CarImporter { |
| 74 | + private int carsImported; |
| 75 | + |
| 76 | + public void SetImportedCars(int carsImported) |
| 77 | + { |
| 78 | + // Update private field from public method |
| 79 | + this.carsImported = carsImported; |
| 80 | + } |
| 81 | +} |
| 82 | +``` |
| 83 | + |
| 84 | +[fields]: https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html |
| 85 | +[methods]: https://docs.oracle.com/javase/tutorial/java/javaOO/methods.html |
| 86 | +[this]: https://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html |
| 87 | +[new]: https://docs.oracle.com/javase/tutorial/java/javaOO/objectcreation.html |
| 88 | +[void]: https://en.wikibooks.org/wiki/Java_Programming/Keywords/void |
| 89 | +[creating-objects]: https://docs.oracle.com/javase/tutorial/java/javaOO/objectcreation.html |
| 90 | +[public]: https://en.wikibooks.org/wiki/Java_Programming/Keywords/public |
| 91 | +[private]: https://en.wikibooks.org/wiki/Java_Programming/Keywords/private |
| 92 | +[default-values]: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html |
| 93 | +[camel-case]: https://techterms.com/definition/camelcase |
| 94 | +[pascal-case]: https://techterms.com/definition/pascalcase |
| 95 | +[encapsulation]: https://en.wikipedia.org/wiki/Encapsulation_(computer_programming) |
| 96 | +[so-getters-setters]: https://stackoverflow.com/questions/2036970/how-do-getters-and-setters-work |
0 commit comments