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

  • Home
  • SEARCH
  • 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 8437685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:30:09+00:00 2026-06-10T07:30:09+00:00

I have a class with two constructors – one accepts a Date object and

  • 0

I have a class with two constructors – one accepts a Date object and the other attempts to create a date object based upon a given timestamp string. The caveat of this is that the conversion to a Date object can throw an exception. I’m getting the ‘variable timestamp might not have been initialized’ error.

First constructor:

public Visit(Date timestamp) {
    this.timestamp = timestamp;
}

Second constructor (the one that produces the error):

public Visit(String timestamp) {
    try {
        this.timestamp = dateFormat.parse(timestamp);
    } catch (ParseException ex) {
        Logger.getLogger(Visit.class.getName()).log(Level.SEVERE, null, ex);
    }
}

I’ve tried adding the initialization of this.timestamp to the finally statement of the try but this then gives an error that the variable may already have been initialized.

  • 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-10T07:30:11+00:00Added an answer on June 10, 2026 at 7:30 am

    If you are happy to use a default value when there is an exception, you can do something like:

    Date temp = null;
    try {
        temp = dateFormat.parse(timestamp);
    } catch (ParseException ex) {
        Logger.getLogger(Visit.class.getName()).log(Level.SEVERE, null, ex);
    }
    
    this.timestamp = (temp == null ? <some default Date value> : temp);
    

    If not, then you could throw an exception from your constructor. Typically, if the argument of your constructor is not valid, you could rethrow an IllegalArgumentException for example.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class with two constructors, one that takes no arguments and one
Given I have a class with two constructors: public class TestClass { ObjectOne o1;
I have a base class, B, which has two constructors, one with no paremeters
I have the following two constructors in my base class: protected ExternalSystemException( params Object[]
Morning. Issue: I have a class called Reports. Two constructors. One allows no parameters,
I have a Person object with two constructors - one takes an int (personId),
I have a class with two constructors (C#). Here is the code snippet: public
I have a class with two constructors that look like this: public MyClass(SomeOtherClass source)
take two following classes and their constructors as samples: class One{ public: One(int a,int
I've defined a sub-class and want to have two constructors with different parameters. It

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.