java静态类和非静态类_关于java:静态和非静态内部类的区别?
我正在閱讀有效的Java 2 -項(xiàng)目22,它在標(biāo)題中寫道:
"Favor static member classes over non-static"
但是在這一章的結(jié)尾
Implementations of the collection interfaces, such as Set and List,
typically use nonstatic member classes to implement their iterators:
// Typical use of a nonstatic member class
public class MySet extends AbstractSet {
... // Bulk of the class omitted
public Iterator iterator() {
return new MyIterator();
}
private class MyIterator implements Iterator {
...
}
}
我做了一個(gè)測(cè)試程序,看看它們之間是否有任何區(qū)別,這里就是。
public class JavaApplication7 {
public static void main(String[] args) {
// TODO code application logic here
JavaApplication7 t = new JavaApplication7();
Inner nonStaticObject = t.getAClass();
Sinner staticObject = new JavaApplication7.Sinner();
nonStaticObject.testIt();
staticObject.testIt();
}
public Inner getAClass(){
return new Inner();
}
static class Sinner{
public void testIt(){
System.out.println("I am inner");
}
}
class Inner{
public void testIt(){
System.out.println("I am inner");
}
}
}
輸出是
I am inner
I am inner
所以,他們做了同樣的工作。
我想知道為什么在這個(gè)例子中使用非靜態(tài)類?
迭代器通常需要首先引用用于創(chuàng)建它的集合。可以使用一個(gè)顯式提供了對(duì)集合的引用的靜態(tài)嵌套類來實(shí)現(xiàn)這一點(diǎn),也可以只使用一個(gè)隱式引用的內(nèi)部類。
基本上,如果嵌套類的每個(gè)實(shí)例都需要一個(gè)封閉類的實(shí)例來操作(而該實(shí)例不變),那么您也可以將其設(shè)置為一個(gè)內(nèi)部類。否則,將其設(shè)置為靜態(tài)嵌套類。
區(qū)別在于,非靜態(tài)內(nèi)部類對(duì)包含類具有隱式引用。
public class JavaApplication7 {
//You can access this attribute in non-static inner class
private String anyAttribute;
public Inner getAClass(){
return new Inner();
}
static class Sinner{
public void testIt(){
//Here, you cannot access JavaApplication7.this
}
}
class Inner{
public void testIt(){
//Here, you can access JavaApplication7.this
//You can also access *anyAttribute* or call non-static method getAClass()
}
}
}
static與非靜態(tài)嵌套類的區(qū)別在于,非靜態(tài)嵌套類隱式地與外部類的一個(gè)實(shí)例相關(guān)聯(lián),它們可以稱為OuterClassName.this。此引用在實(shí)現(xiàn)迭代器時(shí)很有用,因?yàn)樗鼈冃枰L問與其相關(guān)的集合的成員。您可以通過使用static嵌套類來實(shí)現(xiàn)相同的事情,該類顯式地將引用傳遞給外部類。
In the case of creating instance, the instance of non s
static inner class is created with the reference of
object of outer class in which it is defined……this
means it have inclosing instance …….
But the instance of static inner class
is created with the reference of Outer class, not with
the reference of object of outer class…..this means it
have not inclosing instance…
For example……
class A
{
class B
{
// static int x; not allowed here…..
}
static class C
{
static int x; // allowed here
}
}
class Test
{
public static void main(String… str)
{
A o=new A();
A.B obj1 =o.new B();//need of inclosing instance
A.C obj2 =new A.C();
// not need of reference of object of outer class….
}
}
沒有靜態(tài)內(nèi)部類,它是靜態(tài)嵌套類。"非靜態(tài)[嵌套]類的每個(gè)實(shí)例都隱式地與其包含類的封閉實(shí)例關(guān)聯(lián)…可以在封閉實(shí)例上調(diào)用方法。"
靜態(tài)嵌套類無(wú)權(quán)訪問封閉實(shí)例。
參考文獻(xiàn):這條索線
總結(jié)
以上是生活随笔為你收集整理的java静态类和非静态类_关于java:静态和非静态内部类的区别?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java注解定义时间格式_SpringB
- 下一篇: java空指针处理例子_被同事的空指针硬