CLASSES IN JAVA
DEFINITION
A class is the definition of an object type. A class specifies the name and type of object instance variables, but does not specify their value. A class specifies the methods of its objects. An object of a class is an instance of the class. Let us start with an example that consists of defining an ContoCorrente class. As you see in the figure such a class has a balance variable(attribute) which, as we shall see later, is of type private and three public methods. In the rightmost part you can see three instances of that class account1, account2 and account3 and their respective balances. All will become clearer later in the article.
A class is a model for a set of objects that are then instances of this class. In it are defined attributes (variables) and methods that represent actions performed on objects. An attribute represents a property of a class. It describes a set of values that the property can have when objects of that particular class are instantiated. A class can have zero or more attributes. Some members are relative to the instances of the class, and of these each instance will receive a specific and individual allocation, and others that are relative to the class itself, that is, they are class members. A class is created with the keyword class followed by a name involving Camel-Case notation (each word beginning in uppercase). The class definition is then enclosed in a body (body of code delimited by curly brackets)
THE ATTRIBUTES
Within a class we can insert attributes that are variable declarations. Attributes that are not initialized still receive default values.
After defining attributes, a class can define a set of methods.
Each method has a name, as is the case with attributes, it unlike attributes represents an action, in addition, each method has a number of input parameters, a set of information that must be provided to the method each time it is invoked. It may also receive no parameters, in any case a pair of round brackets is required after the name. Finally, each method must indicate a return type in the example void means it returns nothing, double returns a double.
STATIC MEMBERS
There may be members in a class that are not meant for individual instances but for the class itself. They have a different purpose than instance members. Let us resume the example by leaving out instance members.
The modifier static effectively makes the conti attribute a class variable, as well the methods inc() and dec() being preceded by the keyword static are methods or class members. Every single object when it is created is given all instance members. These attributes are specific to each instance. Instead, the class itself maintains a single copy of the static attributes.
PUBLIC AND PRIVATE MEMBERS
Let us return to look at the general pattern of the ContoCorrente class. Based on the principle of information hiding, a portion of attributes and methods should be kept private, that is, not exposed outside the class in which they are defined. However, it is appropriate to expose some of the class members to go to form what is called a public interface. In the example shown, you can see that we have omitted the access modifiers.
Access modifiers applied at the attribute and method level allow us to set the public or private visibility of an attribute or method. The modifier. private specified for the balance attribute indicates that the member is private and usable only within the class. No other method outside the ContoCorrente class will be able to read or change its value.
The modifier public vice versa can be used both within the class and in different classes. In doing so, we have defined the public interface of the ContoCorrente class. If we omit the access modifiers the default values are similar to public, but with one limitation, they are visible only within their own package.
INSTANTIATE A CLASS
We begin by discussing the second category of Data Types the reference types since all classes are handled as reference types. You declare an object of the class having the same class name as if it were a normal variable. To physically create the object we use the operator new operator, which is used to allocate memory for the object we are instantiating. We can also condense the declaration and instantiation of an object into a single instruction.
ContoCorrente cc = new ContoCorrente();
Or do it separately:
ContoCorrente cc; (statement)
cc = new ContoCorrente(); (instantiation)
After this instruction the object variable cc is an instance of the class ContoCorrente.
We defined and created a new object of the class ContoCorrente and assigned it to the variable cc.
VALUES AND REFERENCES TYPES
We begin with the definition (declaration and initialization) of a primitive type, an integer. What happens is that an area of memory is reserved that is suitable for holding an integer, (4 Bytes) and within the memory the number 10 is represented in binary form. If we assign to a second variable y the value of x Java sets up an additional memory area also of 4 Bytes that contains the entire y; The two memory areas are completely separate, in fact a copy of the value of the variable x is made in the memory area occupied by y.
In other words, the memory value that is associated with variable x is copied, duplicated in the memory area associated with variable y. If we now go to change the value we assigned to x the variable y continues to retain its value precisely because a copy is made.
REFERENCE TYPE
Let us now see what happens with objects, which are reference types. Due to the effect of the operator new operator, an area of memory sufficient to hold all the instance attributes of the ContoCorrente class and its methods is dynamically allocated. This memory area is not directly associated with cc as was the case with primitive types, actually cc points to an area of memory that contains the address where the data resides. To be more precise, the variable cc contains a reference to the actual object that is actually located elsewhere, in a different memory area.
We have two variables cc and cc2 each of which contains a reference to some object. We assign the variable cc to the variable cc2. There is only one object that has two references, any change to the instance attributes made through the cc variable affects cc2. Basically a copy of the reference is made. For the object that remains unreferenced, the Garbage Collector comes into action, which we will see later.
LINKS TO PREVIOUS POSTS
LINK TO CODE ON GITHUB
EXECUTION OF THE EXAMPLE CODE
- Download the code from GITHUB, launch the JAR file with the following command in Visual Studio Code, locating in the directory containing the JAR.
java -jar –enable-preview CourseJava.jar
- Or run the main found in the file CorsoJava.java.
Leave A Comment