Summary: Support for things that should only be done once.
Some things should only be done once. For example, initialization of something applicable to an entire class or something to be done the first, and only the first, time a method is invoked.
public class X
{
once
{
// statement block only executed once
// the semantics should be different than
// those of static class initialzers.
}
public X()
{
once
{
// code executed first time this constructor is called
}
}
public void M1()
{
once
{
// code executed first time this method is invoked
}
...
if (some condition)
{
once
{
// only first time this code block executes
}
...
}
while (some condition)
{
once
{
// code to execute only once (i.e. only
// first loop iteration per M1 call)
}
}
}
This is a shortcut for code that in the old C/C++ days would have been controlled by a class or code
static flag indicating whether or not a particular block of code (usually some sort of initialization) had been executed.