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.

Class methods
Operations on objects in Java are called methods. Methods are like math functions: they can take argumentsand return a value. On the other hand, methods in Java also have access to all fields of an object.

To create a method in a class, you must include it in the class. For example, we can define a print(age) method that will print information about our book and display a warning if the user is not yet old enough for that book.

    class Book
    {
        String name;
        String authorName;
        int ageRequirement;
        String text;
        int pageCount;
        // create a print method
        void print(int age)
        {
            System.out.< span style="color:#7d9029">println("Name: " +name);
            System.out.< span style="color:#7d9029">println("Author: " +authorName);
            System.out.< span style="color:#7d9029">println("Number of pages: "+Integer.toString(ageRequirement);
            // verification code goes here
        }
    }
 
Let's analyze the syntax for creating a method.
1) The first line of a method is its signature.
2) The return type of the method is written first. Our method does not return any value, so we write void.
3) Then in the signature is the name of the method (print).
4) In brackets there is a listing of arguments. Arguments are separated by commas. For each argument, its type and name are specified, separated by a space. In our case, there is only one argument, it has the type int and the name age, so there are no commas.
5) After that comes the method code in curly braces. To return a value from a method, write return <value>;. In the example, the method does not return anything, so return can be omitted. To prematurely terminate the execution of a method, you can write return;.
6) In the class itself, we can refer to the fields of the object of this class by name.

Access modifiers
By default, all fieldsand methods of a class in Java are private. This means that no one can access them, which means that outside the objects of this class, no method can use the fields and methods of objects of this class.

Fields and methods can be made public with the access modifier public. There is also a private modifier that makes the field private. It is optional as all fields and methods are private by default. Here is an example of using the public  and private modifiers. class Book { public Stringname; String authorName; private int ageRequirement; Stringtext; public int pageCount; int getTextLength() { return text length(); } public int getAverageLetterCount() { return getTextLength() / pageCount; } private int getDifficuiltyLevel() { return 5 * ageRequirement * text.Length(); } }
In this version of the Book class, the fields name and pageCount are made public for reading and modification in other objects. The getAverageLetterCount() method is also available to be called from objects of other classes. All other fields and methods remain private and are available only in the methods of this class. In the public method getAverageLetterCount() we can call the private method getTextLength() because getAverageLetterCount() belongs to the class itself. But it won't work from a method of another class.

But why then make the fields private? In Java code, you will mostly only see private fields. The fact is that if access to private fields is carried out through the public methods of the object, then with any such access to private fields it will be possible to perform additional actions and checks. More about this will be in the lesson about encapsulation.

The lessons so far have discussed creating classes, which are templates for creating objects. In the same lesson, the process of creating and using the objects themselves will be described.

Links
In the Java language, a variable can never store an object. Instead, Java has references that point to the location of an object in memory.
The syntax for creating a variable of type object reference is as follows:
<Class name> <variable name>;
It is worth noting that we immediately indicate the class whose objects will be referenced.

Example: a variable named "b" pointing to the Book class:

Book b;

In the same way, you can create class fields (you can add an access modifier there). You can create arrays of references and return them from methods. Example:

public class Libary {
  public Book[] books;
  public Book findBookWithName(String name) {
//code that finds the right book
  }
};


Conclusion: in many ways, you can work with references in the same way as with variables of other elementary types (int, long, double...).