Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would you instantiate a Date with -1 as the argument?

Tags:

I know this may be an obvious question, but I am looking at this code:

private Date lastActivity = new Date(-1); 

And am curious what this does - the Date class has six different constructors, and only one can take a single argument of long, like so:

public Date(long date) 

which:

Allocates a Date object and initializes it to represent the specified number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT.

So is -1 just a placeholder? I appreciate any tips or advice.

like image 281
Caffeinated Avatar asked Jun 03 '12 20:06

Caffeinated


People also ask

What does it mean to instantiate a variable?

Instantiation refers to the allocation of memory to create an instance of a class whereas initialization refers to naming that instance by assigning the variable name to that instance.

What method is called to instantiate an object?

Instantiating an Object new requires a single argument: a call to a constructor method. Constructor methods are special methods provided by each Java class that are responsible for initializing new objects of that type. The new operator creates the object, the constructor initializes it.

What happens when you instantiate an object?

To instantiate is to create such an instance by, for example, defining one particular variation of an object within a class, giving it a name and locating it in some physical place.

What is the difference between declaration and instantiation an object?

declaring is just what you have shown. like int a; you declared an int named a but you still not initialized it. instantiate is for class objects. means create a new object with new reference.


2 Answers

it is January 1, 1970, 00:00:00 GMT minus one millisecond (negative numbers are dates in back of the beginning of the epoch)

like image 182
Op De Cirkel Avatar answered Oct 11 '22 11:10

Op De Cirkel


One would typically do this to get a "well known" Date, perhaps for validation or comparisons (all "real Dates" are greater than this one), as a "marker object" to indicate that the Date is really unknown/illegal/pending but you don't want to use null for some reason, or perhaps for a Unit Test.

O.K., looking up the more proper terms for what I called a "marker value", I get sentinel value, flag value, trip value, rogue value, signal value, or dummy data. see wikipedia article here

like image 34
user949300 Avatar answered Oct 11 '22 11:10

user949300