Copy Constructors In Java have the same name as the class name, and they do not have any return value like int, float, and decimal. They are very useful in creating objects for a class. Copy Constructor In Java creates the object of an already registered constructor and executes it. Java language is an Object-Oriented Programming Language that supports constructors. A constructor is usually assigned when there is no value assigned to the respective variables. A constructor makes sure that is done. A copy constructor is generally called to initialize the null values in the variable.
A constructor is automatically called at the time of the creation of the object. A copy constructor is easily created when creating the Java Program. In this article, I will present the copy constructor in the Java Blue J platform. Like in the case of a constructor, a copy constructor also doesn’t have a return type, and it has the same name as the class name. It can be a parameterized or a non- parameterized constructor. Copy Constructor is used to creating an exact copy of an object with the same values as an existing object. The constructor doesn’t have any return value. The void is considered one of the return value types, so even a void is not allowed as one of the constructors’ return values.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Working of a Copy Constructor In Java
The working of a Copy Constructor is very similar to that of a constructor. It copies the values of that of a constructor and creates an object. Initially, a class is made in BlueJ by giving the class name. The types of the class name can be variable. It can be an abstract class, Interface, an Applet, a Unit test, or an Enum or Enumeration. So the class name is given accordingly, and the constructor’s name is given, which is the same as the class name. This is how we initialize the class in the Java Blue J platform.
In the below-mentioned program, there are two constructors that are made. The two constructors have the same name as the class name, and they do not have any return type. The comments section clearly depicts the two concerned constructors that are being initialized. They are also variables made inside the constructors that are called “re” and “I’m.” The first constructor is a parameterized constructor having “re” as double and “im” as double. With the help of this(), the variables are assigned the values that the user wants to enter. Again another constructor, which is called the copy constructor, is created, also known as Complex like that in the first case. Whenever the copy constructor is called, it prints a message called “Copy Constructor called.” And it stores the values in the object “c”. There is a toString() that also returns a value in the main(). It has a specific printing format that is assigned and that will be printed.
The below code is shown to be run on the BlueJ platform. BlueJ is one of the most famous platforms for writing code in the Java language. Code can be very easily written and incorporated into Blue J to get desired outputs and results.
The code below shows the main() and the Strings args[] used to prevent any unknown arguments and disturb the process. The comments sections clearly depict the different constructors being initialized. The first object is “c1”, where the values 10 and 15 are passed through the parameterized constructor. Next, object “c2” is also created, and object c1 is passed through the parameterized constructor. Finally, object “c3” is also created with the value of “c2’ in it. Then the c2 is printed and using the print function.
The below code shows the main()being called. The main() would create an object, and if the void main()is called, the program shows the respective output. There are also options to open the editor, inspect the code that we have written, and make changes to suitable code that can be executed on the platform. Also, the following code can be removed from the BlueJ platform by selecting the Remove option on the platform.
Whenever we click on the code, we get the output very smoothly as given. The output smoothly prints the output in a format that has been given in the print statement. The brackets were also given in the print statement.
So the copy constructor is used to make a duplicate of the constructor in an object. Copy constructors are useful in the fact that they can be used to make a clear photocopy of the object of the constructor that we intend to make. Another sample code and output are shown in the below diagram.
The rectangle() asks two values for the length and breadth of the rectangle. The values entered are 6 and 5, respectively.
The value of the area of the rectangle is given in the output tab, as shown below. It returns the value of 30 as the area of the rectangle.
Conclusion
We notice very patiently through this code that the Blue J platform can run constructors as well as copy constructors. In simple terms, a copy constructor is used to create an object of an already existing constructor. Copy constructors are used in many of the programming languages like C++ and Java. The sample Java code clearly depicts the output which has been shown. The “Copy Constructor Called” message has been clearly displayed, and the format of the printing is also shown.
In the second code, the area of the rectangle is printed, whose output is shown above. The Copy Constructor is not used in many programs except for places where a series of complex print statements are there. It is a concept quite similar to recursion, which is used in Java and many of the programming languages. Copy, as the name suggests, is a photocopy of the constructor being used. The article shows and summarizes how in a programming language like BlueJ, a constructor is made, which has the same name as the class name, and a copy or duplicate of it is also created to print a specific statement.
Recommended Article
This has been a guide to Copy Constructor In Java. Here we discuss the introduction to copy constructor in Java, and it’s working. You can also go through our other suggested articles to learn more –
- Best Java Compilers
- Constructor and Destructor in Java
- Copy Constructor in C++
- Constructor in Java
The above is the detailed content of Copy Constructor In Java. For more information, please follow other related articles on the PHP Chinese website!

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于平衡二叉树(AVL树)的相关知识,AVL树本质上是带了平衡功能的二叉查找树,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要整理了Stream流的概念和使用的相关问题,包括了Stream流的概念、Stream流的获取、Stream流的常用方法等等内容,下面一起来看一下,希望对大家有帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 English version
Recommended: Win version, supports code prompts!

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Zend Studio 13.0.1
Powerful PHP integrated development environment