Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9159857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:35:45+00:00 2026-06-17T13:35:45+00:00

I have a public constructor which takes a parameter (int age) to create an

  • 0

I have a public constructor which takes a parameter (int age) to create an object. I want to check if the passed parameter is legal or not, for example age cannot be negative. If its illegal, then don’t create an object/instance. If legal, no problem.

I can only think of one way to do this –

Make constructor private. Create a static method with parameter (int age) to do all the checking and return a null if you pass it an illegal value. If you pass it a legal value, then create an object and return its reference.
Is there any other way of doing it ? Maybe from inside the constructor itself ?

EDIT :
I thought of one problem with the above method. The factory method/object creator method can only be a static method for obvious reasons. What happens if the factory method has to access a member variable (to do some checking) to create an object ? Then, we will be forced to make that member variable static. This may not be okay in all cases.

Does it make sense ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T13:35:46+00:00Added an answer on June 17, 2026 at 1:35 pm

    Is there any other way of doing it ? Maybe from inside the constructor itself ?

    Yes. I suggest to throw an Exception from constructor

    public class Person
    {
    
    
        int age;
        public Person(int age) throws Exception
        {
           if(age <= 0)
           {
    
              throw new Exception("Age is not allowed");
           }
           // Do some stuffs
           this.age = age;
        }
    
    }
    

    Edit:

    You can also use IllegalArgumentException as suggested by Till Helge Helwig

    public class Person
    {
    
    
        int age;
        public Person(int age) throws IllegalArgumentException
        {
           if(age <= 0)
           {
    
              throw new IllegalArgumentException("Age is not allowed");
           }
           // Do some stuffs
           this.age = age;
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class which takes an IEnumerable constructor parameter which I want to
I have a struct which takes 3 named parameters in to the constructor... public
I have a constructor which has a non-interface dependency: public MainWindowViewModel(IWorkItemProvider workItemProvider, WeekNavigatorViewModel weekNavigator)
I have a user control named ActivityGrid which takes a list of object as
I have a junit test method that takes a CommonsMultipartFile object as a parameter.
We have a class Command which takes an Action as a parameter in the
I have an Assembly(A) which defines a Managed class which has a public constructor
I have a service with an following constructor: public ShimmerService(Context context, Handler handler) {
I have this code to get the default constructor: Public Function f(ByVal t As
I have a constructor question for C#. I have this class: public partial class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.