`
qqdwll
  • 浏览: 138231 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Static key word and synchronzed

    博客分类:
  • Java
阅读更多
There will be times when you will want to define a class member that will be used independently of any object of that class. Normally a class member must be accessed only in conjunction with an object of its class. However, it is possible to create a member that can be used by itself, without reference to a specific instance. To create such a member, precede its declaration with the keyword static. When a member is declared static, it can be accessed before any objects of its class are created, and without reference to any object. You can declare both methods and variables to be static. The most common example of a static member is main( ). main( ) is declared as static


because it must be called before any objects exist.
Instance variables declared as static are, essentially, global variables. When objects of its class are declared, no copy of a static variable is made. Instead, all instances of the class share the same static variable.

Methods declared as static have several restrictions:

They can only call other static methods.
They must only access static data.
They cannot refer to this or super in any way. (The keyword super relates to
inheritance.)
If you need to do computation in order to initialize your static variables, you can declare a static block which gets executed exactly once, when the class is first loaded. The following example shows a class that has a static method, some static variables, and a static initialization block:

// Demonstrate static variables, methods, and blocks.
class UseStatic {
static int a = 3;
static int b;
static void meth(int x) {
System.out.println("x = " + x);
System.out.println("a = " + a);
System.out.println("b = " + b);
}
static {
System.out.println("Static block initialized.");
b = a * 4;
}
public static void main(String args[]) {
meth(42);
}
}

As soon as the UseStatic class is loaded, all of the static statements are run. First, a is set to 3, then the static block executes (printing a message), and finally, b is initialized to a * 4 or 12. Then main( ) is called, which calls meth( ), passing 42 to x. The three println( ) statements refer to the two static variables a and b, as well as to the local variable x.

Note It is illegal to refer to any instance variables inside of a static method. Here is the output of the program:

Static block initialized.
x = 42
a = 3
b = 12

Outside of the class in which they are defined, static methods and variables can be used independently of any object. To do so, you need only specify the name of their class followed by the dot operator. For example, if you wish to call a static method from outside its class, you can do so using the following general form:

classname.method( )

Here, classname is the name of the class in which the static method is declared. As you can see, this format is similar to that used to call non-static methods through object reference variables. A static variable can be accessed in the same way—by use of the dot operator on the name of the class. This is how Java implements a controlled version of global functions and global variables.
Here is an example. Inside main( ), the static method callme( ) and the static variable b are accessed outside of their class.

class StaticDemo {
static int a = 42;
static int b = 99;
static void callme() {
System.out.println("a = " + a);
}
}
class StaticByName {
public static void main(String args[]) {
StaticDemo.callme();
System.out.println("b = " + StaticDemo.b);
}
}
Here is the output of this program:
a = 42
b = 99




Remember that a block of code can acquire three types of lock :
■none at all
■an "instance lock", attached to a single object
■a "static lock", attached to a class
If a method is declared as synchronized , then it will acquire either the instance lock or the static lock when it is invoked, according to whether it is an instance method or a static method.
Acquiring the instance lock only blocks other threads from invoking a synchronized instance method ; it does not block other threads from invoking an un-synchronized method, nor does it block them from invoking a static synchronized method.

Similarly, acquiring the static lock only blocks other threads from invoking a static synchronized method ; it does not block other threads from invoking an un-synchronized method, nor does it block them from invoking a synchronized instance method.

The two types of lock have similar behaviour, but are completely independent of each other.

Outside of a method header, synchronized(this) acquires the instance lock. The static lock can be acquired outside of a method header in two ways :

■synchronized(Blah.class), using the class literal
■synchronized(this.getClass()), if an object is available
分享到:
评论

相关推荐

    要static还是不要static?

    4. **内部类与静态内部类(inner classes and static nested classes)** 在 `InnerInterfaceNotStaticTest.java` 文件中,我们可能会遇到内部类与静态内部类的区别。内部类可以访问外部类的私有成员,但需要与外部...

    Java版SMS4和Base64加密算法

    private static byte[] encode16(byte[] plain, byte[] key); private static byte[] decode16(byte[] cipher, byte[] key); private static byte[] encode32(byte[] plain, byte[] key); private static byte[] ...

    HVDC and FACTS Controllers - Applications of Static Converters in Power Systems

    HVDC and FACTS Controllers: Applications of Static Converters in Power Systems focuses on the technical advances and developments that have taken place in the past ten years or so in the fields of ...

    W. R. Smyth-Static and Dynamic Electricity

    静态场的求解,哈林顿的书中引用到

    类的static,const,static const,const static成员的初始化

    类的static、const、static const、const static成员的初始化 在 C++ 编程中,static、const、static const 和 const static 是四个常见的概念,但它们的初始化方式却容易弄混淆。下面我们将对这四种成员变量的初始...

    java将html转word并将图片打进word内,离线可看

    完整项目包括所有jar包,完美支持html转word,并解决图片断网不能访问的问题。调用HtmlToWord类测试即可,注意修改调用文件的路径,因里面用到jacob,需要将jacob.dll拷贝到项目所在jre\bin目录下面

    Java读取Word文档页数

    在Java编程环境中,读取和处理Word文档通常涉及到使用第三方库,因为Java标准库并不直接支持Word文件的操作。本主题将深入探讨如何利用给定的`itextpdf-5.5.9.jar`和`jacob.jar`这两个库来实现Java读取Word文档页数...

    HDR Static Metadata Extensions(HDR标准)

    This standard specifies static High Dynamic Range (HDR) metadata extensions using an additional InfoFrame and EDID CTA data block, replacing previously reserved codes in Table 5 and Table 46 of CTA-...

    .Net几种加密方法(固定key 随机key 对称加密 MD5等)

    public static string Encrypt(string plainText, byte[] key) { // 创建加密对象 using (Aes aes = Aes.Create()) { // 设置密钥和初始向量 aes.Key = key; aes.IV = new byte[16]; // 使用默认的IV // ...

    C#读取Word内容

    在C#编程环境中,读取Word文档内容是一项常见的任务,特别是在处理数据导入、报告生成或者文档处理的应用中。本文将详细讲解如何使用C#来读取Word文档的内容,并提供源码示例。 首先,要读取Word文档,我们需要借助...

    static和const用法详解

    Static和Const用的详解 static和const是C语言中两个非常重要的关键字,它们在程序设计中扮演着不同的角色。本文将详细介绍static和const的用法和特性。 static static是C语言中的一个存储类型限定符,主要用来...

    poi解析导入word (简单Demo使用)

    Apache POI是一个流行的Java库,专门用于处理Microsoft Office格式的文件,如Word、Excel和PowerPoint。在"poi解析导入word (简单Demo使用)"这个主题中,我们将深入探讨如何使用Apache POI库来读取、修改和创建Word...

    java视频 static关键字

    在Java编程语言中,`static`关键字是一个非常重要的修饰符,它有多种用途,涉及到类、对象以及变量和方法的生命周期。本视频教程详细讲解了`static`关键字在Java中的应用及其背后的原理。 首先,我们要理解`static`...

    Static Analysis of Plates and Bridges Using Finite Prism Method

    Static Analysis of Plates and Bridges Using Finite Prism Method,ABD ELGADIR ELZIEN,,The combination of Fourier series and finite element expansion is used, in finite prism method, to construct ...

    java html转换word

    Apache POI是Java库,专门用于处理Microsoft Office格式的文件,如Word(.doc和.docx)、Excel(.xls和.xlsx)等。在这个小例子中,我们将探讨如何使用Apache POI将HTML转换为Word文档。 首先,我们需要了解Apache ...

    static关键字详解

    static关键字详解: 内容摘要: 一、static关键字定义属性 二、static属性与非static属性还有一个最大的区别,所有的非static属性必须产生实例化对象之后才可以访问,但是static属性不受实例化对象的控制,也就是 说...

    Static静态变量的应用(VB6.0代码编写)

    在VB6.0编程环境中,Static变量是一种特殊的变量类型,它在函数或过程中扮演着持久化存储的角色。Static变量的特点在于,即使函数或过程执行完毕,它的值也不会被清除,而是保留下来,等待下一次调用时继续使用。这...

    java给word文档插入水印并设置保护密码

    在Java编程环境中,处理Word文档是一项常见的任务,特别是在开发OA(办公自动化)系统时。本文将详细介绍如何使用Java为Word文档插入水印以及设置保护密码,以确保文档的安全性。 首先,我们需要一个能够操作...

    final和static用法详解JAVA

    Java 中的 final 和 static 关键字详解 final 关键字是 Java 中的一种访问修饰符,用于修饰类、方法和变量,以确保它们不能被修改或继承。下面是 final 关键字的详细解释: 1. final 类 final 类不能被继承,也...

Global site tag (gtag.js) - Google Analytics