- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java copy constructor
Java has no explicit copy constructor but we can mimic the behaviour. See the following example −
Example
public class Tester { private String message; public Tester(String message){ this.message = message; } public Tester(Tester tester){ this.message = tester.message; } public String getMessage(){ return message ; } public void setMessage(String message){ this.message = message; } public static void main(String[] args) { Tester tester = new Tester("Welcome"); System.out.println(tester.getMessage()); Tester tester1 = new Tester(tester); System.out.println(tester1.getMessage()); } }
- Related Articles
- Why do we need a copy constructor and when should we use a copy constructor in Java?
- Copy Constructor in C++
- Virtual Copy Constructor in C++
- What is a copy constructor in C#?
- Copy constructor vs assignment operator in C++
- When is copy constructor called in C++?
- Difference Between Copy Constructor and Assignment Operator in C++
- Java default constructor
- Java parameterized constructor
- Constructor overloading in Java
- Enum constructor in Java
- Default constructor in Java
- Deep Copy and Shallow Copy in Java
- Is constructor inherited in Java?
- Constructor Chaining In Java programming

Advertisements