Results 1 to 2 of 2
  1. #1
    derrickD is offline Member
    Join Date
    Apr 2007
    Location
    USA
    Posts
    50
    Rep Power
    0

    Default Constructor Chaos

    I am having a situation where sometimes the parameter sent to instantiate one of my classes is null. It is not, howver , calling the constructor I thought it would call. So, I made a test case and sure enough I am not getting what I thought.
    Java Code:
    public class MyClass
    {
      private MyClass()
     {
       System.out.println("No param sent");
      }
    	
      private MyClass(Object o)
      {
    	System.out.println("Object");
      }
    
      private MyClass(double[] dArray)
     {
       System.out.println("double array");
      }
    
      public static void main(String[] args)
     {
    	new MyClass(null);
      }
    }
    I thought it would call the constructor that has no signature but it appears to call the double[] dArray constructor.

  2. #2
    pegitha is offline Member
    Join Date
    Apr 2007
    Location
    Indiana
    Posts
    83
    Rep Power
    0

    Default

    Overload selects the most specific constructor. I guess it is detemining that the double dArray constructor is the most specific. Maybe you should avoid all these overloads or at least make them more specific (i.e. do not use Object in that one).
    p

Similar Threads

  1. Constructor calling
    By ravian in forum New To Java
    Replies: 2
    Last Post: 12-22-2007, 07:53 PM
  2. Replies: 0
    Last Post: 12-19-2007, 10:10 AM
  3. Replies: 0
    Last Post: 12-19-2007, 10:01 AM
  4. Constructor Help
    By bluegreen7hi in forum New To Java
    Replies: 2
    Last Post: 11-15-2007, 06:44 AM
  5. Help with constructor in java
    By mathias in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 02:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •