Why design ThreadLocalMap as static nest class in ThreadLocal?

Why design ThreadLocalMap as Static Class in ThreadLocal ? While static and non-static nested classes have differences below. Nested static class doesn't need reference of Outer class, but Non-static nested class or Inner class requires Outer class reference. Inner class(or non-static nested class) can access both static and non-static members of Outer class. A static class cannot acces

为什么在ThreadLocal中将ThreadLocalMap设计为静态嵌套类?

为什么设计ThreadLocalMap作为静态类中ThreadLocal ? 虽然静态和非静态嵌套类在下面有差异。 嵌套静态类不需要Outer类的引用,但非静态嵌套类或Inner类需要Outer类引用。 内部类(或非静态嵌套类)可以访问Outer类的静态和非静态成员。 静态类不能访问Outer类的非静态成员。 它只能访问Outer类的静态成员。 如果没有外部类的实例,并且Inner类可以引用在其嵌套的Outer类中定义的数据和方法,则不能创建Inner类的实例,

Static nested class has full access to private outer class members?

Update: I'm still a unclear about this. I'm trying to find the JLS to describe this behaviour. Instead, I found this quote in 8.3 of the JLS: Member class declarations (§8.5) describe nested classes that are members of the surrounding class. Member classes may be static, in which case they have no access to the instance variables of the surrounding class; or they may be inner clas

静态嵌套类可以完全访问私有外部类成员?

更新: 我对此还不清楚。 我试图找到JLS来描述这种行为。 相反,我在JLS的8.3中找到了这个引用: 成员类声明(第8.5节)描述了作为周围类成员的嵌套类。 成员类可能是静态的,在这种情况下,它们无法访问周围类的实例变量; 或者他们可能是内部类(§8.1.3)。 这是不是意味着嵌套静态类不应该访问外部类变量? 我在哪里可以找到行为应该是什么的澄清? 结束更新 我正在通过一个静态嵌套类的实例来澄清外部类的私有

Why can outer Java classes access inner class private members?

I observed that Outer classes can access inner classes private instance variables. How is this possible? Here is a sample code demonstrating the same: class ABC{ class XYZ{ private int x=10; } public static void main(String... args){ ABC.XYZ xx = new ABC().new XYZ(); System.out.println("Hello :: "+xx.x); ///Why is this allowed?? } } Why is this behavio

为什么外部Java类可以访问内部类私有成员?

我观察到外部类可以访问内部类私有实例变量。 这怎么可能? 以下是一个演示相同的示例代码: class ABC{ class XYZ{ private int x=10; } public static void main(String... args){ ABC.XYZ xx = new ABC().new XYZ(); System.out.println("Hello :: "+xx.x); ///Why is this allowed?? } } 为什么允许这种行为? 内部类只是一种清楚地区分真正属于原始外部类的某些功能的方法

Static nested class in Java, why?

I was looking at the Java code for LinkedList and noticed that it made use of a static nested class, Entry . public class LinkedList<E> ... { ... private static class Entry<E> { ... } } What is the reason for using a static nested class, rather than an normal inner class? The only reason I could think of, was that Entry doesn't have access to instance variables, so from an

Java中的静态嵌套类,为什么?

我正在查看LinkedList的Java代码,并注意到它使用了静态嵌套类Entry 。 public class LinkedList<E> ... { ... private static class Entry<E> { ... } } 什么是使用静态嵌套类,而不是一个正常的内部类的原因是什么? 我能想到的唯一原因是Entry无法访问实例变量,所以从OOP的角度来看,它具有更好的封装性。 但我认为可能还有其他原因,或许是表现。 它可能是什么? 注意。 我希望我的条款正确,我将

Why can I access the private members of an enclosing class reference

I have seen many questions about accessing private members of an enclosing class. However, my question is the opposite. If I have (as an example), the following code: public class A { private String outerString = "silly string"; static class B { private final A someA = new A(); public void foo() { String b = someA.outerString ; } } } I'm wonde

为什么我可以访问封闭类引用的私有成员?

我看到很多关于访问封闭类的私人成员的问题。 但是,我的问题恰恰相反。 如果我有(例如),下面的代码: public class A { private String outerString = "silly string"; static class B { private final A someA = new A(); public void foo() { String b = someA.outerString ; } } } 我想知道为什么这个编译? 由于我从类A(通过someA.outerString)访问'outerStri

Static nested class has access to private constructor of outer class

It is stated that: A static nested class interacts with the instance members of its outer class (and other classes) just like any other top-level class. In effect, a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience. So how to explain this static nested class has access to private constructor of its enclosing class

静态嵌套类可以访问外部类的私有构造函数

据说: 静态嵌套类与其外部类(和其他类)的实例成员交互,就像任何其他顶级类一样。 实际上,为了方便打包,静态嵌套类在行为上是嵌套在另一个顶级类中的顶级类。 那么如何解释这个静态嵌套类可以访问它的封闭类的私有构造函数呢? 那么如何解释这个静态嵌套类可以访问它的封闭类的私有构造函数呢? 因为任何private成员(字段,构造函数或方法)的范围和可访问性都是其声明类的完整体,包括任何嵌套类。 从Java语言

Inner Classes and Enclosing Instances in java?

While reading java Doc i got following things . An inner class C is a direct inner class of a class O if O is the immediately lexically enclosing class of C and the declaration of C does not occur in a static context So if I write public class O{ class Inner1{ // } class Inner2{ } } My Question is out of these two Inner class which one is the direct Inner Class of O

java中的内部类和外壳实例

在阅读java Doc时,我得到了以下几点。 内部类C是类O的直接内部类,如果O是C的立即包含在内的类,并且C的声明不会出现在静态上下文中 所以,如果我写 public class O{ class Inner1{ // } class Inner2{ } } 我的问题是出自这两个内在阶级哪一个是O的直接内部阶级? 还有一些我到达那里的要点。 2,O类是C类的第n个词汇封闭类,如果它是第n-1个词汇封闭类C的直接封闭类。 3.类O的直接内部

Accessibility of inner class members from enclosing class

I thought members of an inner class, even when declared private, is accessible from its enclosing class. But I am running into compile time error with the following code structure. My top level class implements Iterable interface. The Iterator is implemented in an inner class. When an instance of the inner class is obtained through the iterator() method, I am not able to access the data field

来自封闭类的内部类成员的可访问性

我认为内部阶层的成员,即使在宣布为私人阶层的时候,也可以从其封闭的阶级中获得。 但我遇到了编译时错误,下面的代码结构。 我的顶级类实现了Iterable接口。 迭代器在内部类中实现。 当通过iterator()方法获得内部类的实例时,我无法使用该实例访问数据字段。 public class RandomQueue<Item> implements Iteralbe<Item> { public RandomQueue() {} public Iterator<Item> iter

Understanding local classes in Java

As know, local classes are inner classes. JLS 14.3 All local classes are inner classes (§8.1.3). What an inner class is is JLS 8.1.3 An inner class C is a direct inner class of a class or interface O if O is the immediately enclosing type declaration of C and the declaration of C does not occur in a static context. A class C is an inner class of class or interface O if it is either a d

了解Java中的本地类

众所周知,本地类是内部类。 JLS 14.3 所有本地类都是内部类(第8.1.3节)。 内心阶层是什么 JLS 8.1.3 如果O是C的直接封闭类型声明并且C的声明不在静态上下文中,则内部类C是类或接口O的直接内部类。 类C是类或接口O的内部类,如果它是O的直接内部类或O的内部类的内部类。 这意味着内部类的声明不应该出现在非静态的上下文中。 但考虑下面的程序: public static void main (String[] args) throws java.lang.Exc

Anonymous classes enclosing instances

I'm reading Joshua Blochs "Effective Java" 2nd edition. Currently I'm at item 22 which describes inner and nested classes but I can't understand what does he mean by this sentence: Anonymous classes have enclosing instances if and only if they occur in a nonstatic context. Can someone give me an example of code and explain what does it exactly do ? I know that if Inner

包含实例的匿名类

我正在阅读Joshua Blochs的“Effective Java”第2版。 目前我在第22项中描述内部类和嵌套类,但我无法理解这句话的含义: 当且仅当它们出现在非静态上下文中时,匿名类才会封装实例。 有人能给我一个代码的例子,并解释它到底做了什么? 我知道,如果InnerClass是其成员OuterClass它的外围实例是OuterClass ,但在annonymous类方面,它有些陌生。 public static void main(String[] args) { Runnable r = new Runnable()