JavaRush /Java Blog /Random EN /Coffee break #114. The default constructor in Java is an ...

Coffee break #114. The default constructor in Java is an example of a class constructor. Maven vs Gradle: How to Choose the Right Build Tool

Published in the Random EN group

Default Constructor in Java - Example of a Class Constructor

Source: FreeCodeCamp In this article we will talk about constructors, how to create them and what are the default constructors in Java. Coffee break #114.  The default constructor in Java is an example of a class constructor.  Maven vs Gradle: How to Choose the Right Build Tool - 1

What is a constructor?

As a class-based object-oriented programming term, a constructor is a unique method used to initialize a newly created object (class). There are several rules that you should follow when creating constructors. These rules include:
  • The constructor name must match the class name.
  • A constructor must not have a return type.
Before we continue, let's see what a class looks like in Java:
public class Student {
  String firstName;
  String lastName;
  int age;
}
The above code shows a Student class with three attributes - firstName , lastName and age . We will assume that the class should be a model for student registration. Recall that these three attributes do not have any values, so the information in them is not hard-coded. Now we will use constructors to create a new instance of our Student object :
public class Student {
  String firstName;
  String lastName;
  int age;

  //конструктор Student
  public Student(){
      firstName = "Ihechikara";
      lastName = "Abba";
      age = 100;
  }

  public static void main(String args[]) {
      Student myStudent = new Student();
      System.out.println(myStudent.age);
      // 100
  }
}
We created a constructor that we used to initialize the attributes defined in the Student object . The above code is an example of a no-argument constructor . Let's now look at a different kind of example:
public class Student {
  String firstName;
  String lastName;
  int age;

  //конструктор
  public Student(String firstName, String lastName, int age){
      this.firstName = firstName;
      this.lastName = lastName;
      this.age = age;
  }

  public static void main(String args[]) {
    Student myStudent = new Student("Ihechikara", "Abba", 100);
    System.out.println(myStudent.age);
  }

}
Now we have created a parameterized constructor. A parameterized constructor is a constructor created with arguments/parameters. Now let's change it.
public Student(String firstName, String lastName, int age){

  }
We've created a new constructor that takes three arguments - two strings and an integer.
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
We then associated these arguments with the attributes we defined when creating our class. Now we have initialized the Student object using the constructor.
public static void main(String args[]) {
    Student myStudent = new Student("Ihechikara", "Abba", 100);
    System.out.println(myStudent.age);
  }
Finally, we created a new instance of the Student object and passed our arguments to it. We were able to do this because we had already defined them in the constructor. I created one constructor with three arguments, but you can also create separate constructors to initialize each attribute. Now that you know what a constructor is in Java and how to use it, let's look at default constructors.

What is the default constructor?

The default constructor is the constructor generated by the compiler if we do not define any constructors for the class. Here's an example:
public class Student {
  String firstName;
  String lastName;
  int age;

  public static void main(String args[]) {
      Student myStudent = new Student();

      myStudent.firstName = "Ihechikara";
      myStudent.lastName = "Abba";
      myStudent.age = 100;

      System.out.println(myStudent.age);
      //100

      System.out.println(myStudent.firstName);
      //Ihechikara
  }
}
Can you find the difference between this and the two previous examples? Note that before creation, we did not define a myStudent constructor to initialize the attributes created in the class. This will not throw an error in our path. More precisely, the compiler will create an empty constructor, but you will not see this constructor anywhere in the code - it happens “under the hood”. This is what the above code will look like when the compiler starts doing its job:
public class Student {
  String firstName;
  String lastName;
  int age;


  /* пустой конструктор, созданный компилятором. Этот конструктор не будет отображаться в вашем codeе*/
  Student() {

  }

  public static void main(String args[]) {
      Student myStudent = new Student();

      myStudent.firstName = "Ihechikara";
      myStudent.lastName = "Abba";
      myStudent.age = 100;

      System.out.println(myStudent.age);
      //100

      System.out.println(myStudent.firstName);
      //Ihechikara
  }
}
Many people confuse the default constructor with the no-argument constructor, but in Java they are not the same thing. Any constructor created by the programmer is not considered a default constructor in Java.

Conclusion

In this article, we learned what constructors are and how we can create and use them to initialize our objects. We also talked about default constructors and how they differ from no-argument constructors. Happy coding!

Maven vs Gradle: How to Choose the Right Build Tool

Source: Hackernoon Build automation is an important aspect of software development. In this article, we will compare two of the most popular build tools for Java development: Maven and Gradle . Coffee break #114.  The default constructor in Java is an example of a class constructor.  Maven vs Gradle: How to choose the right build tool - 2

Make and Apache Ant

Previously, developers used the Make tool to create Java projects, and the building process was not much different from creating applications in any other language. But in 2000, the An t (Another Neat Tool) build system was released. Ant, like Make, uses an imperative style, and its build scripts have XML syntax. Ant is designed as a build automation system for Java projects. Therefore, Java developers can easily extend its functionality.

Maven

In 2004, the new Maven build system came out and changed the process of building Java applications. Previously, developers themselves organized a folder structure to store source code, resources, classpath directories, and output directories. Because of this, Ant build scripts for two different applications could be very different: compilation, assembly, copying files to the output directory, etc. were written separately. In Maven, a Java project always has a clear structure. For example, sources should be in src/main/java, resources for tests should be in src/test/resources. Maven allows you to create the file structure of a typical project with a single command. Maven also introduces the concept of a “build lifecycle” with sequential phases: validate ➞ compile ➞ test ➞ package ➞ verify ➞ install ➞ deploy) Now, thanks to the fixed structure folders and a set of targets, there is no need to write and maintain a large build script - they have become declarative. It has become more convenient for developers to work not only with their own code, but also with third-party projects, because it is clear how the source code works and how to assemble it. There are quite a few libraries in the Java world, and large applications use hundreds of them. If you use Ant, remember that you will have to add the necessary jar files to the project yourself. You also need to take care of the necessary transitive dependencies. Maven provides dependency manager functionality through the Maven Central Repository. Now, when specifying a new dependency in the build script, Maven will automatically find the required jar of the corresponding version and all its transitive dependencies, download them and make sure that they end up in the project’s classpath. You can also maintain your own private repository, where you can store your own or patched libraries, or standard libraries created by hand. It should be noted that Ant can be used in conjunction with the Apache Ivy Project , which also allows you to manage dependencies and work with Maven repositories. Despite all the advantages of Maven, its declarative approach can be a disadvantage in some situations. For example, when you need to change the build lifecycle and add new goals to the build process. Maven functionality can be extended using plugins. There are many ready-made Maven plugins that solve different problems, all of which are also available from the Maven Central Repository. But if for some reason you need to slightly change the standard life cycle, and there is no suitable plugin, the developer will have to create it himself.

Gradle

The first release of the Gradle build system was released in 2008. 4 years later version 1.0 was released. The goal of the Gradle project is to retain all the benefits of Maven, but at the same time increase the ability to customize the build process. Gradle build scripts are written in Groovy DSL. Gradle allows you to write declarative build scripts and is more compact than Maven because XML is quite bulky. You can easily add custom logic to your Gradle build process. All you need to do is write a Groovy script, and you don't need to develop plugins. The developer can easily debug the execution of build scripts since they are regular Groovy files. Thus, Gradle combines declarative and imperative approaches. Gradle also supports plugins, allowing developers to change settings. One of the significant advantages of Gradle is incremental builds. When the build is restarted, Gradle determines whether the target's input files have changed, and if not, the target's execution is skipped because its output artifacts have already been built. This gives a significant increase in build speed compared to Maven, especially in large multi-module projects. Gradle can also run the Gradle daemon, a background process that avoids wasting resources and initialization time each time the build is run. Gradle has a handy Gradle wrapper feature - the ability to generate shell and Windows command scripts that automatically download the Gradle distribution of the specified version and use it to build the project. This means that to build a Gradle project you don't need to install Gradle separately, just install Java. If necessary, you can easily switch your project to a different version of Gradle.

Choosing between Maven and Gradle

Despite the benefits of Gradle, quite a few projects use the Maven build system. The choice depends on the type of project and team. Maven has been in use since 2004, so more developers are familiar with it. Moreover, Maven is stable. The latest major version 3 was released in 2010. Gradle has already changed significantly several times without backward compatibility, and developers have had to port their build scripts to new versions. Since not everyone is familiar with Groovy or Kotlin, working with Gradle requires additional knowledge, whereas Maven uses clear XML. This raises the question: if the project began development before Gradle became popular, does it make sense to move the build scripts to Gradle? On the one hand, more and more developers are choosing Gradle. For example, Spring, Hibernate and LinkedIn use Gradle. The Android build system is also Gradle, and it is generally popular among Android application developers. On the other hand, all well-known IDEs have integration with both build systems and support autocompletion when editing build scripts. Like Maven, Gradle has a huge number of plugins that allow you to add frequently used functions to the project build process.

Conclusion

From the pros and cons of each of the assembly systems described above, the following conclusion can be drawn. Maven is more suitable for small projects that do not require customization of the build process, and for which the project build time is not so critical. Gradle is more suitable for large-scale projects with a large number of modules, as well as for Android applications.
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION