- 浏览: 138214 次
- 性别:
- 来自: 上海
-
文章分类
最新评论
-
qq466862016:
不错的文章
JDK动态代理与CGLIB代理的对比 -
jinxiongyi:
你好,jpedal pdf转换图片的 画质,怎么提高。。我转 ...
介绍几款PDF转图片的开源工具 -
qqdwll:
转图片消耗的内存还是不小。 有时间得找找有没有更好的办法, 把 ...
介绍几款PDF转图片的开源工具 -
xiaoyao3857:
Thanks for your work!It's help ...
Keeping Eclipse running clean (转载) -
iceside:
图片讲解非常详细,说清了引用复制是怎么回事
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
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
发表评论
-
介绍几款PDF转图片的开源工具
2011-09-09 00:40 4653最近项目中有个需求需要把PDF转成一张图。经过调查,有 ... -
jadclipse(反编译Eclipse插件)
2011-07-19 19:13 1677Jad Java decompiler plugin for ... -
Java开发时候的内存溢出
2011-07-13 17:33 1210这里以tomcat环境为例,� ... -
class loader
2011-07-08 17:23 0Because Class.getResource() eve ... -
Jakarta-Common-BeanUtils使用笔记
2011-07-06 16:55 1432原文转发http://blog.csdn.net/fa ... -
基于MVC模式Struts框架研究
2011-04-13 20:02 1377不做web开发多年了, 可偶尔去面试的时候, 还是 ... -
Java反射与动态代理
2011-04-13 15:08 1043这篇文章是 成富 先生在InfoQ上Java 深度历险系列的一 ... -
Java枚举类型
2011-04-04 19:50 808Tiger中的一个重要新特性是枚举构造,它是一种新的Java枚 ... -
Java 值传递的终极解释
2011-03-21 22:49 2004对于Java的值传递, 你真的了解么? Ja ... -
六种异常处理的陋习
2011-03-20 03:21 854你觉得自己是一个Java专 ... -
数组初始化
2011-03-20 02:40 912数组初始化,你觉得简单吗? a.如果你觉得简单,那请看下面的 ... -
Java 实现 hashCode 方法
2011-03-11 17:07 1241原文 http://www.javapractices.com ... -
Java 中 immutable class 以及怎样实现immutable 类
2011-03-11 16:47 1388原文 http://www.javapractices.com ... -
Java 内部类介绍
2011-02-16 17:14 1033转载: http://zhidao.baidu.com/que ... -
Java 中的Clone 学习总结
2011-01-25 18:22 28091. 一个类需要实现clone. 一个最佳实践是它需要实现 C ... -
java 通过流, nio 移动文件或者文件夹
2011-01-04 17:54 1907我们用例子说明java怎样通过不同的方式移动文件或文件夹。 ... -
转 深入探讨SOAP、RPC和RMI
2010-12-17 00:34 1101这篇文章是从网上转下来的。 原文应该是写于2001年。 10 ... -
java 6 中的性能优化
2010-12-07 15:30 1462文章转载自: http://www ... -
创建强健,稳定的 JMS 系统
2010-12-07 15:21 1008The most reliable way to produc ... -
Java Modifier Summary
2010-11-12 15:10 921<tbody> <tr> ...
相关推荐
4. **内部类与静态内部类(inner classes and static nested classes)** 在 `InnerInterfaceNotStaticTest.java` 文件中,我们可能会遇到内部类与静态内部类的区别。内部类可以访问外部类的私有成员,但需要与外部...
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 focuses on the technical advances and developments that have taken place in the past ten years or so in the fields of ...
静态场的求解,哈林顿的书中引用到
类的static、const、static const、const static成员的初始化 在 C++ 编程中,static、const、static const 和 const static 是四个常见的概念,但它们的初始化方式却容易弄混淆。下面我们将对这四种成员变量的初始...
完整项目包括所有jar包,完美支持html转word,并解决图片断网不能访问的问题。调用HtmlToWord类测试即可,注意修改调用文件的路径,因里面用到jacob,需要将jacob.dll拷贝到项目所在jre\bin目录下面
在Java编程环境中,读取和处理Word文档通常涉及到使用第三方库,因为Java标准库并不直接支持Word文件的操作。本主题将深入探讨如何利用给定的`itextpdf-5.5.9.jar`和`jacob.jar`这两个库来实现Java读取Word文档页数...
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-...
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文档的内容,并提供源码示例。 首先,要读取Word文档,我们需要借助...
Static和Const用的详解 static和const是C语言中两个非常重要的关键字,它们在程序设计中扮演着不同的角色。本文将详细介绍static和const的用法和特性。 static static是C语言中的一个存储类型限定符,主要用来...
Apache POI是一个流行的Java库,专门用于处理Microsoft Office格式的文件,如Word、Excel和PowerPoint。在"poi解析导入word (简单Demo使用)"这个主题中,我们将深入探讨如何使用Apache POI库来读取、修改和创建Word...
在Java编程语言中,`static`关键字是一个非常重要的修饰符,它有多种用途,涉及到类、对象以及变量和方法的生命周期。本视频教程详细讲解了`static`关键字在Java中的应用及其背后的原理。 首先,我们要理解`static`...
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 ...
Apache POI是Java库,专门用于处理Microsoft Office格式的文件,如Word(.doc和.docx)、Excel(.xls和.xlsx)等。在这个小例子中,我们将探讨如何使用Apache POI将HTML转换为Word文档。 首先,我们需要了解Apache ...
static关键字详解: 内容摘要: 一、static关键字定义属性 二、static属性与非static属性还有一个最大的区别,所有的非static属性必须产生实例化对象之后才可以访问,但是static属性不受实例化对象的控制,也就是 说...
在VB6.0编程环境中,Static变量是一种特殊的变量类型,它在函数或过程中扮演着持久化存储的角色。Static变量的特点在于,即使函数或过程执行完毕,它的值也不会被清除,而是保留下来,等待下一次调用时继续使用。这...
在Java编程环境中,处理Word文档是一项常见的任务,特别是在开发OA(办公自动化)系统时。本文将详细介绍如何使用Java为Word文档插入水印以及设置保护密码,以确保文档的安全性。 首先,我们需要一个能够操作...
Java 中的 final 和 static 关键字详解 final 关键字是 Java 中的一种访问修饰符,用于修饰类、方法和变量,以确保它们不能被修改或继承。下面是 final 关键字的详细解释: 1. final 类 final 类不能被继承,也...