According to the JLS: 15.9.5 Anonymous Class Declarations An anonymous class declaration is automatically derived from a class instance creation expression by the compiler. An anonymous class is never abstract (§8.1.1.1). An anonymous class is always an inner class (§8.1.3); it is never static (§8.1.1, §8.5.2). An anonymous class is always implicitly final (§8.1.1.2) . This seems like it
根据JLS: 15.9.5匿名类声明匿名类声明是由编译器从类实例创建表达式自动派生的。 匿名类从不抽象(§8.1.1.1)。 一个匿名类永远是一个内部类(§8.1.3); 它永远不会是静态的(§8.1.1,§8.5.2)。 一个匿名类总是隐式地结束(§8.1.1.2) 。 这似乎是一个具体的设计决定,所以它有可能会有一些历史。 如果我选择这样的课程: SomeType foo = new SomeType() { @Override void foo() { super.foo();
The Java Tutorial says that since an inner class is associated with an instance of the enclosing class, it (the inner class) cannot define any static members itself. It's interesting for me why can't inner classes declare static members for instance, some private static field, which the instance of this inner class could possibly share with the other instances of the same inner class?
Java教程指出,由于内部类与封闭类的实例关联,因此它(内部类)不能自己定义任何静态成员。 有趣的是,为什么内部类不能声明静态成员,例如一些private static字段,这个内部类的实例可能与同一个内部类的其他实例共享? 这只是Java中的事物的实现,必须被认为是理所当然的或者其他的东西? 基本上只是一个任意的决定。 没有理由不支持,但也没有任何理由支持它。 只需在外部类中声明静态字段即可。 此外,该引用可能
Alright, I know it's the rule: According to JLS: 8.1.3 Inner Classes and Enclosing Instances, inner classes may not declare static initializers or member interfaces. Inner classes may not declare static members, unless they are compile-time constant fields. According to 8.5.2 Static Member Type Declarations, "Member interfaces are always implicitly static. It is permitted but not re
好吧,我知道这是规则: 根据JLS:8.1.3内部类和Enclosing实例,内部类可能不会声明静态初始值设定项或成员接口。 内部类可能不会声明静态成员,除非它们是编译时常量字段。 根据8.5.2静态成员类型声明,“成员接口总是隐式静态的,允许但不要求声明成员接口来明确列出静态修改器”。 他们永远是顶级的,而不是内在的。 我只是想知道为什么。 如果我们被允许在内部类中声明接口,会发生什么? 如果我把它放到另一个Class
This question already has an answer here: Why is an anonymous inner class containing nothing generated from this code? 5 answers If you write the code like, public class Outer { private class Inner {} } You will note that there is only one constructor private Outer$Inner(Outer) This constructor is required by Section 8.8.9 of the JLS, which says that if no constructor is defined a
这个问题在这里已经有了答案: 为什么一个匿名的内部类没有包含这段代码生成的任何内容? 5个答案 如果你写这样的代码, public class Outer { private class Inner {} } 你会注意到只有一个构造函数是private Outer$Inner(Outer) 这个构造函数是JLS第8.8.9节要求的,它说如果没有定义构造函数,就必须生成一个默认的构造函数,在这种情况下,默认的构造函数必须是私有的, 在类类型中,如果该类声明为public,
What is the difference between List<? super T> List<? super T> and List<? extends T> List<? extends T> ? I used to use List<? extends T> List<? extends T> , but it does not allow me to add elements to it list.add(e) , whereas the List<? super T> List<? super T> does. extends The wildcard declaration of List<? extends Number> foo3 Li
List<? super T>什么区别List<? super T> List<? super T>和List<? extends T> List<? extends T> ? 我曾经使用List<? extends T> List<? extends T> ,但它不允许我添加元素给它list.add(e) ,而List<? super T> List<? super T> 。 extends List<? extends Number> foo3的通配符声明 List<? extends Number> foo3意味着任何这些都是合法的作业
I have a Hashmap that, for speed reasons, I would like to not require locking on. Will updating it and accessing it at the same time cause any issues, assuming I don't mind stale data? My accesses are gets, not iterating through it, and deletes are part of the updates. Yes, it will cause major problems. One example is what could happen when adding a value to the hash map: this can cause
我有一个Hashmap,出于速度的原因,我想不要求锁定。 假设我不介意陈旧的数据,是否会更新它并同时访问它会导致任何问题? 我的访问是获取,而不是遍历它,删除是更新的一部分。 是的,这会造成重大问题。 一个例子是当向哈希映射添加一个值时会发生什么:这可能导致表的重新哈希,并且如果在另一个线程迭代碰撞列表(哈希表“桶”)时发生这种情况,该线程可能会错误地无法找到地图中存在的密钥。 HashMap对于并发使用显然
I've found strange behavior of java concurrency. See on the next code below: public class Test { static CountDownLatch latch = new CountDownLatch(1); public static void main(String[] args) throws UnsupportedEncodingException, InterruptedException { final Thread t = new MyThread(); t.start(); synchronized (t) { latch.c
我发现java并发性的奇怪行为。 请参阅下面的代码: public class Test { static CountDownLatch latch = new CountDownLatch(1); public static void main(String[] args) throws UnsupportedEncodingException, InterruptedException { final Thread t = new MyThread(); t.start(); synchronized (t) { latch.countDown(); Syste
Is it correct to say that static means one copy of the value for all objects and volatile means one copy of the value for all threads? Anyway a static variable value is also going to be one value for all threads, then why should we go for volatile? Declaring a static variable in Java, means that there will be only one copy, no matter how many objects of the class are created. The variable wi
说静态意味着所有对象的值的一个副本,并且volatile意味着所有线程的值的一个副本是否正确? 无论如何,一个静态变量值也将成为所有线程的一个值,那么为什么我们应该挥霍? 在Java中声明静态变量意味着只有一个副本,无论该类创建了多少个对象。 即使没有创建Objects该变量也是可访问的。 但是,线程可能具有本地缓存的值。 当一个变量是易失性的而不是静态的时候 ,每个Object会有一个变量。 所以,从表面上看,它
Currently I can't understand when we should use volatile to declare variable. I have do some study and searched some materials about it for a long time and know that when a field is declared volatile, the compiler and runtime are put on notice that this variable is shared and that operations on it should not be reordered with other memory operations. However, I still can't understand
目前我无法理解什么时候应该使用volatile来声明变量。 我已经做了一些研究并且搜索了很长时间的一些资料,并且知道当一个字段被声明为volatile时,编译器和运行时会注意到这个变量是共享的,并且它的操作不应该与其他内存重新排序操作。 但是,我仍然无法理解我们应该在什么情况下使用它。 我的意思是可以有人提供任何示例代码,可以证明使用“易失性”带来好处或解决问题相比,而不使用它? 这是volatile为什么是必需的一
I have a thread: class Foo extends Thread { boolean active = true; public void run() { while(active) { //do stuff } } public void end() { active = false; } public void hibernate() { synchronized(this) { wait(); } } } If another thread calls end() , will Foo immediately see
我有一个线程: class Foo extends Thread { boolean active = true; public void run() { while(active) { //do stuff } } public void end() { active = false; } public void hibernate() { synchronized(this) { wait(); } } } 如果另一个线程调用end() , Foo立即看到active现在是否为fal