`
d02540315
  • 浏览: 32514 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
最近访客 更多访客>>
社区版块
存档分类
最新评论

Static Initializers and Lazy Instantiation

阅读更多
Why use Lazy Instantiation when static initialization is more efficient? Well, there are pros and cons to both, and there is no firm answer.

The Singleton article gave one solution for implementing global variables, but it wasn't the only solution. Java does, in fact, have global variables of a sort, but they're called class variables. Here is an example:

"The most important reason for using Lazy Instantiation is keeping resources under control. "
  • Listing 1

static MyObject object1 = new MyObject()

In Listing 1, the static keyword is used to indicate that this particular variable, namely object1, is a class variable rather than an instance variable. This means that there will be only one copy of the variable for the class rather than one for each instance of the class. The code segment in Listing 1 not only creates the variable, it also contains a static initializer that gives the object a value. This static initialization occurs automatically when the class containing the variable is first accessed.

Here, first accessed is defined to be the first time one of the following events occurs:

An instance of the class is created via a constructor.
A static method that is defined in the class (not inherited) is called.
A static variable that is declared in the class (not inherited) is assigned or otherwise accessed. This does not include the static initializer, which occurs at compile time.


The process of Lazy Instantiation, on the other hand, doesn't automatically allocate variables. It enables the creation of objects to be put off until they are actually needed. Instead of being created when the program first starts, the creation of variables managed via Lazy Instantiation is deferred until they are actually required.

Which is better? Lets start off with some examples.

Here is an example of static initialization:
  • Listing 2

public class MyClass {
  static MyObject object1 = new MyObject();
  void doProcess() {
    object1.process();
  }
}

In Listing 2,object1 is created and its value is initialized when the class
MyClass is first accessed. After some period of time,object1 will be used via the
doProcess() method.
Here is a similar code segment making use of Lazy Instantiation.
  • Listing 3

public class MyClass {
  MyObject object1;
  void doProcess() {
    If (object1 == null)
      object1 = new MyObject();
    object1.process();
  }
}

The code in Listing 3 performs the same function as in Listing 2 except that the
object1 variable is being managed via Lazy Instantiation. When MyClass is created, nothing is done with object1 other than implicitly setting its reference to null. The doProcess() method makes use of object1 and checks to see if it exists before using it. If it does not exist, an instance is created and then used.

Now back to the big question: Which is better?

Different Purposes
When it comes to speed, the static version wins. Static methods are quicker than normal ones, as there is no code execution overhead. Classes implementing Lazy Instantiation contain management code that needs to be executed whenever access to a variable is required. In Listing 3, overhead is incurred every time object 1 is accessed, because it first needs to be checked for a null value.

For ease of programming, static wins again. Only a single line of code is required to implement a variable with a static initializer. As mentioned above, Lazy Instantiation requires that an object be checked for a null value every time it is accessed. Also, multithreaded applications would require use of the double-checked locking pattern, which results in additional code. (See previous article on Singletons for more information on this.)

Okay, so there are several reasons for using a static initializer, as my readers have mentioned. However, there are several good reasons to use Lazy Instantiation instead.

The first reason for Lazy Instantiation concerns the initialization of the managed object's value. For a static initializer, the initial value is determined at compile time. When the developer puts to code the line that creates the object, he or she also determines what the initial value is going to be. While this is fine for many uses, there are times when it just isn't good enough. Lazy Instantiation, however, initializes the value of an object when the object is created. Since the object is being created at runtime, there is much more data available which can be used to calculate an appropriate initial value. One example of this would be the placing of a user name and id in a session variable.

Secondly, items that are static cannot be used to implement an interface. Interfaces in Java are powerful programming constructs that allow classes to inherit method definitions. Static objects suffer a loss of a great deal of programming power by not being able to implement interfaces.

The most important reason of all for using Lazy Instantiation, however, is that of keeping resources under control. Static objects are always created, whether they are needed or not. If the object is never used, then space is wasted holding the object and time is wasted creating it. Objects managed by Lazy Instantiation will only be created if they are needed and do not suffer these problems. This is especially important if the resources used are valuable, such as a database connection. Why tie up a database connection if it is never going to be used?

Also, since static objects are created during the initialization of a Java program, a lot of processing needs to be done before the program can actually begin. Software built with Lazy Instantiation defers the creation of objects and thus starts up a lot faster. This makes the Java program appear more agile and responsive, a definite bonus if you are trying to sell the software to a client.

Summary
The pros and cons of static initializers versus Lazy Instantiation must be weighed to determine which is best for a given use. Both are powerful, and both are examples of good programming, but they serve different purposes.
分享到:
评论

相关推荐

    PowerMock资料大全(E文)

    PowerMock uses a custom classloader and bytecode manipulation to enable mocking of static methods, constructors, final classes and methods, private methods, removal of static initializers and more....

    Static关键字详解.docx

    3. 静态块(Static Initializers): 静态块是一段用于初始化静态变量的代码,它在类加载时执行,且只执行一次。这对于需要在类加载时进行一次性设置的场景非常有用。 ```java public class MyClass { static int...

    第04章 面向对象(上) 10 Static关键字修饰属性和方法

    3. **静态初始化块(Static Initializers)**:`static`还可以修饰初始化块,这种块在类加载时执行一次,用于初始化静态变量。它与实例初始化块(非静态的)的区别在于,实例初始化块在创建对象时执行。 4. **类的...

    Kubernetes Initializers工作原理介绍.pptx

    Kubernetes Initializers是Kubernetes集群中的一种扩展机制,它允许在对象(如Pod、Service等)被正式创建或更新之前,执行预初始化的任务。这种机制主要用于实现更细粒度的控制,确保资源按照特定策略进行管理和...

    ember-load-initializers

    此加载项对app/initializers和app/instance-initializers内部的文件进行迭代,并通过传递文件的解析名称来分别调用app.initializer和app.instanceInitializer方法。安装ember install ember-load-initializers贡献...

    Google C++ Style Guide_英文版.pdf

    - **Unnamed Namespaces and Static Variables:** Unnamed namespaces can be used to create implicitly static local variables that are visible only within the translation unit. - **Non-member, Static ...

    eclipse 格式化模板

    eclipse 的code style 下的cleanup 格式化模板 ...Sort members excluding fields, enum constants, and initializers Organize imports Format source code Correct indentation Remove redundant type arguments

    ‘’JAVA面试题解惑系列.doc

    1. 静态变量(static fields)和静态初始化块(static initializers)会先于实例变量和实例初始化块执行。静态变量的初始化按照它们在源代码中的顺序进行,而静态初始化块则在其定义的位置被执行,通常用于设置静态...

    Linkers & Loaders 英文版原著

    此外,还探讨了特殊段如共同块(common blocks)和初始化与终结化函数(initializers and finalizers)的处理。 通过阅读《Linkers & Loaders》,学习者可以获得对软件构建过程深刻的洞察力,并理解链接器和加载器...

    js-initializers:避免在您的 Rails 应用程序上出现 JS 混乱! js-initializers 是一个添加微框架的引擎,用于以模块化方式初始化和组织客户端逻辑

    )用法将js-initializers添加到您的 Gemfile # Gemfilegem 'js-initializers' 将js-initializers添加到您的链轮清单: //= require js-initializers 等等! 您已准备好添加初始化程序: // app/assets/javascripts/...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    Scoping Namespaces Nested Classes Nonmember, Static Member, and Global Functions Local Variables Static and Global Variables Classes Doing Work in Constructors Default Constructors Explicit ...

    Csharp.6.for.Programmers.6th.Edition

    Integrated coverage of new C# 6 functionality: string interpolation, expression-bodied methods and properties, auto-implemented property initializers, getter-only properties, nameof, null-conditional ...

    Programming Microsoft LINQ in Microsoft.NET Framework 4

    LINQ, Object/Collection Initializers OOP: Classes, Objects, Inheritance, Polymorphism, Interfaces WinForms, WPF, XAML, Event Handling WPF GUI/Graphics/Multimedia Silverlight® Lists, Queues, ...

    C# 2010 for Programmers 4ed part1

    LINQ, Object/Collection Initializers OOP: Classes, Objects, Inheritance, Polymorphism, Interfaces WinForms, WPF, XAML, Event Handling WPF GUI/Graphics/Multimedia Silverlight® Lists, Queues, ...

    Effective C#

    - **Benefits:** Using properties instead of public data members enhances encapsulation and allows for validation logic, lazy loading, and caching without changing the API. - **Implementation:** Define...

    The_CSharp_Programming_Language_Third_Edition

    Most notably, of course, it has been updated to cover all the new features of C# 3.0, including object and collection initializers, anonymous types, lambda expressions, query expressions, and partial...

    PyPI 官网下载 | dodoo-initializer-0.5.1.tar.gz

    标题 "PyPI 官网下载 | dodoo-initializer-0.5.1.tar.gz" 提供的信息表明,这是一个从Python Package Index(PyPI)官方源下载的软件包,名为 "dodoo-initializer",其版本号是0.5.1,并且是以tar.gz格式压缩的。...

    C语言编程常见错误

    ### C语言编程常见错误详解 C语言是一种结构化编程语言,因其高效性和灵活性而被广泛应用于操作系统、嵌入式系统及高性能应用开发等领域。然而,这些特性也为编程者带来了一定的学习曲线,尤其是在处理语法细节时...

    Springer.The.Developer’s.Guide.to.Debugging.2008.pdf

    6.1.8 Measurement with time can have Errors and Variations . 68 6.1.9 Select a Test Case that Exposes the Runtime Bottleneck . . 68 6.1.10 The Difference Between Algorithm and Implementation . . 70 ...

    Beginning Microsoft Visual CSharp 2008 Wiley Publishing(english)

    Initializers 437 Type Inference 443 Anonymous Types 445 Extension Methods 449 Lambda Expressions 455 Summary 466 Exercises 467 Part II: Windows Programming 468 Chapter 15: ...

Global site tag (gtag.js) - Google Analytics