CLASSES IN JAVA
THE CONSTRUCTOR
A constructor is a particular method used to fabricate instances of your class. This method has the same name as the class we want to instantiate, back to the previous example.
We instantiate the class without using the create method, and without initializing the instance attributes.
As you see the instance attributes are valued at the default values, which for numeric types are zeros. We introduce the constructor. One peculiarity of the constructor is that it does not have a return type, plus it usually has parameters that are used to initialize the attributes of its class.
For brevity, I have omitted the code for the view() method, which remains the same. If we now compile the class we get an error because the create method uses an empty constructor that has not been declared.
Java behind the scenes uses an empty default constructor, which it uses whenever we do not define it. However, if we have defined the constructor Java removes this constructor by default and a compile-time error is produced. We can avoid the error by modifying the code in this way.
METHOD OVERLOAD AND POLYMORPHISM
We have the possibility of defining several forms for the same method that differ from each other because of the parameters received. By the term Polymorphism of which method overloading is a prime example, we mean “Having many forms. “ Let us return to the general form of defining a method.
The name and parameter list constitute the signature of a method, within a class we can implement methods that have the same name but different signature. This type of definition is called a method overload.
Pay attention to one thing. The signature of a method refers to the name and list of parameters, not the type returned. Let’s take an example.
We define a main() method. The compiler is able to call the right method based on the list of parameters provided.
METHOD OVERLOAD AND CONVERSION
Let us look at how method overloading behaves in the presence of implicit conversion of primitive types. Let’s go back and look at a simplified version of MyClass.
The first invocation does not require conversion, in the second call the short is promoted to an integer, in the third call to myMethod there is no conversion, the method in overload that accepts a double is called. In the last one this method is always called and the float is promoted to double. The conversions you saw are performed when there is no match between the argument and the parameter.
OVERLOAD CONSTRUCTORS
Just as we can overload methods in the same way we can overload constructors. Let us look again at the ContoCorrente class in which we did not use constructors.
With the constructor we can initialize the balance and create its instance. In previous versions of code we have always used the default constructor, the one created by Java without parameters.
We create the cc instance.
Now we have lost the default constructor, that is, we can no longer write:
ContoCorrente cc = new ContoCorrente();
We can avoid this by using constructor overload.
INVOKE OVERLOADED CONSTRUCTORS
Let us look again at the ContoCorrente class. In practice we place the focus on the two constructors we have defined.
Using the this keyword we can invoke the various constructors within them. The parameterless constructor invokes the constructor that accepts a double parameter. These operations come at a certain cost in terms of performance, so they should not be abused.
INNER CLASS
An inner class is a class defined inside another class; it is a local class with respect to the outermost class. Let us return to the MyClass class. Within an inner class we have direct access to members of the outer class, but not vice versa.
The opposite is not true, that is, an external class does not have access to the members (attributes and methods) of the internal class.
We create a test class to try out the program.
We can go further, we can define an inner class in a method of the outer class. The test program does not change, what has changed is the scope of the inner class visible only in myMethod. Let’s look at the code.
THE STATIC BLOCK
Constructors as we have seen are very useful, however there are circumstances where they are not the tool we need, for example we may want to execute code once when the class is loaded at Runtime, or perform initialization operations on static attributes before using static methods on these attributes. Let’s take the code from MyClass again.
This code is executed only once, typically when the class is brought into memory because we create an instance of it.
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