Skip to main content
added 49 characters in body
Source Link
rgettman
  • 178.7k
  • 30
  • 282
  • 365

Yes, this line leads to a stack overflow error.

Date today = new Date(2016,3,14); 

WhenYou have found what causes it, and here is why. When you create a Date, it has as an instance variable today, which creates another Date, which creates another Date, and so on, until your stack overflows.

Making it static solves the problem, because then today is initialized only once, and not as part of every instance that is created.

You made it as part of another class, which will work, but you can make it static within your own Date class, to make it cleaner, more understandable, and more readable.

Yes, this line leads to a stack overflow error.

Date today = new Date(2016,3,14); 

When you create a Date, it has as an instance variable today, which creates another Date, which creates another Date, and so on, until your stack overflows.

Making it static solves the problem, because then today is initialized only once, and not as part of every instance that is created.

You made it as part of another class, which will work, but you can make it static within your own Date class, to make it cleaner, more understandable, and more readable.

Yes, this line leads to a stack overflow error.

Date today = new Date(2016,3,14); 

You have found what causes it, and here is why. When you create a Date, it has as an instance variable today, which creates another Date, which creates another Date, and so on, until your stack overflows.

Making it static solves the problem, because then today is initialized only once, and not as part of every instance that is created.

You made it as part of another class, which will work, but you can make it static within your own Date class, to make it cleaner, more understandable, and more readable.

Source Link
rgettman
  • 178.7k
  • 30
  • 282
  • 365

Yes, this line leads to a stack overflow error.

Date today = new Date(2016,3,14); 

When you create a Date, it has as an instance variable today, which creates another Date, which creates another Date, and so on, until your stack overflows.

Making it static solves the problem, because then today is initialized only once, and not as part of every instance that is created.

You made it as part of another class, which will work, but you can make it static within your own Date class, to make it cleaner, more understandable, and more readable.