Class fields
The essence of
object-oriented programming is to represent the program as an interaction of
objects.
An object is some kind of entity with certain properties and certain operations on it.
Objects were originally created to simulate reality: many things around us can be represented as an object. For example, a book you read recently can be thought of as an object with the properties 'name', 'author', 'age rating', 'text of the book', 'number of pages', etc. Above the book you can perform operations like "read a book", "burn a book", etc.
In the terminology of the Java programming language, these "properties" are called
fields, and operations on objects are called
methods.
Since Java is a statically typed language, any object must be created according to some pattern. In Java, such templates are
classes.
Class describes what fields a certain type of object can have and what operations are defined on it.
The difference between
class and
object is analogous to the difference between "car" and "Toyota Camry, parked at the 5th house on Cheburekovo Street".
Consider the procedure for creating your own class of objects with fields in Java.
class Book {
stringname;
String authorName;
int ageRequirement;
Stringtext;
int pageCount;
}
This code creates the class"Book
". objectsof class «Book
» there are two integer properties (named «ageRequirement
» and «pageCount
») and three properties of type «String
» (with the names "name
", "authorName
", and "text
".
The general syntax for creating a class with fields is as follows.
class <class name> {
<first property type> <first property name>
<second property type> <second property name>
&helli;
<type of last property> <last property name>
}
Of course, in Java, you can create classes not only in this way (there are also methods, access modifiers, and much more), but more on that later.