Static Nested Classes

As with class methods and variables, a static nested class is associated with its outer class. And like static class methods, a static nested class cannot refer directly to instance variables or methods defined in its enclosing class: it can use them only through an object reference.


Note: 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.


Static nested classes are accessed using the enclosing class name:

OuterClass.StaticNestedClass

  

For example, to create an object for the static nested class, use this syntax:

OuterClass.StaticNestedClass nestedObject =
     new OuterClass.StaticNestedClass();

  

时间: 2024-10-20 07:16:18

Static Nested Classes的相关文章

Java - Nested Classes

(本文参考:http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html) Nested Classes class OuterClass { ...... class StaticNestedClass { ...... } ...... class InnerClass { ...... } 定义 nested classes (嵌套类)分为两种:non-static与static,前者通常称作inner classes,后者称

Java中的Nested Classes和Inner Classes

Java中的Nested Classes和Inner Classes Java有嵌套类(Nested Classes)和内部类(Inner Classes)的概念. 嵌套类(Nested Classes)分为两种:static and non-static,即静态的和非静态的,静态的嵌套类我们称之静态嵌套类(static nested classes),非静态的嵌套类我们称之为内部类(inner classes). 静态的嵌套类我们可以这样表示: OuterClass.StaticNestedC

When to Use Nested Classes, Local Classes, Anonymous Classes, and Lambda Expressions

As mentioned in the section  Nested Classes, nested classes enable you to logically group classes that are only used in one place, increase the use of encapsulation, and create more readable and maintainable code. Local classes, anonymous classes, an

Static Nested Class 和 Inner Class的不同

内部类就是在一个类的内部定义的类,内部类中不能定义静态成员(静态成员不是对象的特性,只是为了找一个容身之处,所以需要放到一个类中而已,这么一点小事,你还要把它放到类内部的一个类中,过分了啊!提供内部类,不是为让你干这种事情,无聊,不让你干.我想可能是既然静态成员类似c语言的全局变量,而内部类通常是用于创建内部对象用的,所以,把“全局变量”放在内部类中就是毫无意义的事情,既然是毫无意义的事情,就应该被禁止),内部类可以直接访问外部类中的成员变量,内部类可以定义在外部类的方法外面,也可以定义在外部类

【Java面试题】11 什么是内部类?Static Nested Class 和 Inner Class的不同。

Inner Class(内部类)定义在类中的类. (一般是JAVA的说法) Nested Class(嵌套类)是静态(static)内部类.(一般是C++的说法)静态内部类:1 创建一个static内部类的对象,不需要一个外部类对象2 不能从一个static内部类的一个对象访问一个外部类对象 Java内部类与C++嵌套类最大的不同就在于是否有指向外部的引用上. 内部类:就是在一个类的内部定义的类, A.非静态内部类中不能定义静态成员(静态对象是默认加载,那么静态内部类应该先于外部类被加载到内存中

java嵌套类(Nested Classes)总结

转自:http://www.cnblogs.com/aigongsi/archive/2012/04/24/2467183.html Nested Classes定义 在java语言规范里面,嵌套类(Nested Classes)定义是: A nested class is any class whose declaration occurs within the body of another class or interface. A top level class is a class t

How to Leak a Context: Handlers & Inner Classes

Consider the following code: public class SampleActivity extends Activity { private final Handler mLeakyHandler = new Handler() { @Override public void handleMessage(Message msg) { // ... } } } While not readily obvious, this code can cause cause a m

java基础之 Advanced Class Design

 java基础之 Advanced Class Design Abstract Classes In many programming situations, you want to specify an abstraction without specifying implementation-level details. In such cases, you can use either abstract classes or interfaces. Abstract classes are

关于Java的静态:静态类、静态方法、静态变量、静态块等

原文地址:Java static keyword - Class, Method, Variable, Block, import - JournalDev 很少看到文章能把静态这个问题解释的很清楚,有些文章绕来绕去很复杂.这篇解释静态问题的小文通透而容易理解.所以我把它翻译一下,让大家能好好理解关于静态. 其实对于Java静态的引入,考虑目标是使用和运行的便捷,不需要再实例化,特别是一些工具性质的代码段.当不用再实例化时,就可以相当于直接使用,增加了公共代码复用的便捷性. 从Java8开始,还