mrentropy
mrentropy

Reputation: 59

Beginner Q: optional arguments in Java

I'm totally new to Java, as in, I started yesterday.

I've got a class that I'd like to have two constructors for: one without arguments, and one with.

Supposedly this should be simple: overload the constructor by writing two methods:

public class sortList {

    public int ncell, npart, cell_n, index, Xref;

    // constructor(s):                                                                                                                                            

    public void sortList() {                                                                                                                                      
        initLists( 1, 1 );
    }

    public void sortList( int ncell_in, int npart_in ) {
        initLists( ncell_in, npart_in );
    }    

    private void initLists( int ncell_in, int npart_in ) {
         /* DO STUFF */
    }
}

When I call this from my main() though:

    sortList mySL = new sortList( 5, 6 );

... java complains:

myDSMC.java:5: error: constructor sortList in class sortList cannot be applied to given types;
          sortList mySL = new sortList( 5, 6 );
                          ^   required: no arguments    
found: int,int    
reason: actual and formal argument lists differ in length
1 error

(For the curious, I am just translating a super-simple DSMC code from C++...).

What silly thing am I missing?

Thanks. -Peter

Upvotes: 1

Views: 59

Answers (2)

J-Alex
J-Alex

Reputation: 7127

Constructors in Java have no return type and have name the same as the name of the class. The all methods in java have return type (void - if nothing to return). You don't have to provide any constructors for your class, but you must be careful when doing this. The compiler automatically provides a no-argument, default constructor for any class without constructors. This default constructor will call the no-argument constructor of the superclass. In this situation, the compiler will complain if the superclass doesn't have a no-argument constructor so you must verify that it does. If your class has no explicit superclass, then it has an implicit superclass of Object, which does have a no-argument constructor.

Example of constructor and methods:

// for this class default no-args constructor implicitly created
public class Test1 {
   int id;

   // regular method
   public int getId() {
       return id;
   }

    // regular method
    public int test1() {
        return 1;
    }
}


public class Test2 {

   int id;

   // no-args constructor
   public Test2() {
   }

   // overloaded constructor
   public Test2(int id) {
      this.id = id;
   }

    // regular method
   public int getId() {
      return id;
   }

   // regular method
   public void test2() {
      System.out.println("1");
   }
}

All defined constructors implicitly call super();. So Test2 constructor actually looks like this:

   public Test2(int id) {
      super();
      this.id = id;
   }

Upvotes: 1

Eran
Eran

Reputation: 393986

This are not constructors, they are regular methods :

public void sortList() {...}
public void sortList( int ncell_in, int npart_in ) {...}

Change them to constructors by removing the return type :

public sortList() {...}
public sortList( int ncell_in, int npart_in ) {...}

Since you didn't declare any constructors in your sortList class (you just declared two regular methods having the same name as the class), only the default parameter-less constructor was available.

Upvotes: 3

Related Questions