简体   繁体   中英

Throw exception for default constructor java

How do I prevent the default constructor from being used in Java?

In my assessment it says:

"We don't want the user to use the default constructor since the user has to specify the HashCode, and maximum load factor"

I thought this would do the trick, but apparently not (dictionary is a class that is used to throw exceptions):

public boolean HashDictionary() throws DictionaryException {}

DictionaryException Class:

public class DictionaryException extends Throwable {

}

Test to make sure it throws an exception when using default contructor(Supplied by lecturer):

try
{
    HashDictionary h = new HashDictionary();
    System.out.println("***Test 1 failed");

}
catch (DictionaryException e) {
        System.out.println("   Test 1 succeeded");
}

I just want to know how I could do this, as I'm not familiar with a method of doing it. Thanks.

You can declare the default one as private if you do not want it to be called.

To answer your comment, you can throw an exception-

public HashDictionary() throws DictionaryException {
    throw new DictionaryException("Default constructor is not allowed.");
}

You can

a) Omit the default constructor

b) Make the default constructor private

c) Throw an exception when the default constructor is used

public HashDictionary() throws DictionaryException {
    throw new DictionaryException("Default constructor should not be used!");
}

Don't do it like that. Just make the default constructor private:

private HashDictionary() {}

EDIT: What was the boolean doing in the Constructor definition? I just copied it...

If you don't want the user to use the default constructor you can either omit the constructor if other non-default constructors are declared, or you can make the default constructor private .

The other issue you may be having is that the declaration you put in your question is not a constructor, it is a method. Constructors do not have return types.

Change this

public boolean HashDictionary() throws DictionaryException {}

to this

public HashDictionary() throws DictionaryException {}

better yet just change it to

private HashDictionary() {}

and then no one can access it outside of the class itself.

I ended up adding this:

 public DictionaryException(String error_string)
 {
     System.out.println(error_string);
 }

as the default constructor for DictionaryException

And then:

public HashDictionary() throws DictionaryException {
    throw new DictionaryException("Default constructor should not be used!");
}

This for the default constructor of the HashDictionary, the issue was that the type was a boolean, so I removed that and it seemed to work.

  1. To Forbid the Default Constructor To Be Called from Outside the Class, use:

    private HashDictionary(){}

  2. To Forbid the Default Constructor To Be Called from Even Inside the Class, use:

    Declare a parameterized constructor and don't write the default non-parameterized constructor inside the class. public HashDictionary(String string){ }

  3. If you want some exception to be thrown while calling out the constructor, then:

    public HashDictionary() throws Exception {
    throw new Exception("throwing exception from constructor");
    }

Regards, Birendra.

If you don't want to use a default constructor but you want to instantiate a class you need to implement a custom constructor. You have to tell the user what is needed to create a valid instance. And that's it! Default constructor won't be available anymore. If a constructor logic is too complicated, feel free to extract private methos, including a private constructor if you feal it's ok in your case. If you have many ways of creating instance you can use static factory-like methods and private ctor. Making a private ctor only to disable the default one makes no sense (at least I don't see one).

If you don't want to instantiate a class (I mean you dont want to use the default ctor and you have no reason to create a custom ctor) this means you want to use only a static fields/methods, so make whole class static.

Oh, I assume that we're talking about non abstract classes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM