Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Copy Constructor

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Copy Constructor

    I've written the following code to test a copy constructor to exit a program if there is no input but the program always runs. If there is no input in the brackets then it simply runs with 0.00.

    public class Imports
    {
    	private double num;
     
    	public Imports()
    	{
    		setNum(0);
    	}
     
    	public Imports(double initialNum)
    	{
    		setNum(initialNum);
    	}
     
     
    	public double setNum(double startNum)
    	{
    		num = startNum;
    		return num;
    	}
     
    	public Imports(Imports copy)
    	{
    		if (copy == null)
    		{
    			System.out.println("Fatal Error.");
    			System.exit(0);
    		}
     
    		num = copy.num;
    	}
     
        public String toString()
     	{
     		return (" Number is " + num);
     	}
     
    	public static void main(String[] args)
    	{
    		Imports acct1 = new Imports();
    		Imports copy = new Imports(acct1);
     
     
    		System.out.println(acct1);
    		System.out.println(copy);
     
    	}
    }

    Can someone please tell me why it isn't quitting with an error message when there is nothing in the brackets of acct1?
    Last edited by helloworld922; April 23rd, 2011 at 10:40 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Copy Constructor

    There's a difference between passing a null parameter and passing nothing at all.

    // this passes nothing at all, not even a null
    Import acct1 = new Imports();
     
    // this passes a null value
    Import acct1 = new Imports(null);

    Notice how you have a constructor which takes nothing at all? That's what gets called when you pass no arguments to the constructor.

    public Imports() // called when you call Imports with no parameters
    {
        setNum(0);
    }

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    Rhyssa6 (April 23rd, 2011)

  4. #3
    Junior Member akashmishra's Avatar
    Join Date
    Aug 2021
    Location
    Vietnam
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Re: Copy Constructor

    So here,

    when this code executed
    Imports acct1 = new Imports();

    It invoked following constructor
    public Imports()
    {
    setNum(0);
    }

    which set num value as 0.0..

    now next when this line of code executed
    Imports copy = new Imports(acct1);

    As it takes already created object accnt1 (having num 0.00) then copy object will be same as acct1.

    Hence both following code will print "Number is 0.0"
    System.out.println(acct1);
    System.out.println(copy);

    Read more about copy Constructor with example program https://www.quizcure.com/java/what-i...n-java-program
    Last edited by akashmishra; August 7th, 2021 at 09:22 PM.

Similar Threads

  1. How to copy J2ME application to a mobile device
    By vishal21 in forum Java ME (Mobile Edition)
    Replies: 2
    Last Post: January 21st, 2012, 05:52 PM
  2. Need Hellp!! problem with org.apache.struts.actions.DownloadAction.copy
    By nikshri in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 6th, 2010, 01:06 AM
  3. how to copy cells to a new sheet of excel with JexcelAPI??
    By 19world in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: June 15th, 2010, 03:49 AM
  4. Copy folder based on latest date
    By ayonsoni in forum File I/O & Other I/O Streams
    Replies: 10
    Last Post: May 17th, 2010, 07:01 AM
  5. program a button that copy whatever in a JTextArea
    By voltaire in forum AWT / Java Swing
    Replies: 5
    Last Post: May 17th, 2010, 12:43 AM