Java Nested Class

Normal class(Non-nested class) can only be public, abstract or final. For nested classes, static is allowed.

Nested classes(private, public, protected) -> 2 Categories.

1. Static Nested Class (Very Different from C# static class)

alpha: Have access to all the static members of its parent class.

beta: Can instantiate objects by new ParentClass.StaticNestClass(). Now allowed to create objects in C#.

gamma: Can have non-static members.

delta: same as gamma. Cannot access ParentClass.this.

2. Non-static nested class(Inner Class)

alpha: Have access to all the members of its parent class.

beta: Can instantiate objects by new ParentClass().new InnerClass().

gamma: Cannot have static members, except for final static fields.

delta: Variable shadowing:

    ParentClass.this.memberName -> Parent class member.

    this.memberName, ParentClass.InnerClass.this.memberName  -> Inner class member

======================================

1. Interface can only be defined inside a top-level class, an interface or in static context because interfaces are inherently static.

2. Anonymous Classes:

  Interface/AbstractClass obj = new Interface/AbstractClass(Empty for interface. Initialize for abstract class){

  String/int ...;

  public void ImplementAbstractMethodA(){...}

  public void ImplementAbstractMethodB(){...}

}; <--Semicolon in the end

时间: 2024-12-13 14:28:29

Java Nested Class的相关文章

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的线程和多线程教程

Java线程(Java Thread)是执行某些任务的一种轻量级进程.Java中的Thread类提供了多线程(multi-threading)功能,应用程序能够创建多个线程并同一时候执行. 在一个应用程序中,一般会有两种类型的线程-用户线程(user thread)和守护线程(daemon thread).当我们启动引用程序时,首先创建用户线程main,我们能够创建多个用户线程和守护线程.当全部的用户线程都运行完成时,JVM就会终止程序. 对不同的线程,我们能够设置对应的优先级,但并不意味着高优

java 嵌套类

如果不需要内部类对象与其外围类对象之间有联系,那么可以将内部类声明为static,这通常称为嵌套类,想要理解static应用于内部类时的含义,就必须记住,普通的内部类对象隐式地保存了一个引用,指向它的外围类对象,然而,当内部类时static时,就不是这样了,嵌套类意味着: 1)要创建嵌套类的对象,并不需要其外围类的对象 2)不能从嵌套类的对象中访问非静态的外围类对象 嵌套类与普通的内部类还有一个区别,普通内部类的字段与方法,只能放在类的外部层次上,所以普通的内部类不能有static数据和stat

查询Elasticsearch嵌套类型数据,且只返回嵌套数据中命中的元素

测试环境 Elasticsearch 6.3 Kibana 6.3 造点测试数据 新建一个index作为测试 以下是一个存储博客文章及其评论的数据结构,评论(comments)是nested类型: PUT /es_blog { "mappings": { "blogpost": { "properties": { "title": { "type": "text" }, "su

nested exception is java.sql.SQLException: IO 错误

1.错误描述 (mx.messaging.messages::ErrorMessage)#0 body = (null) clientId = "18CE3B03-9709-9DA8-7634-340C23317FDD" correlationId = "3161DF25-D056-D694-E5CF-963E4FE036D9" destination = "waitController" extendedData = (null) faultC

nested exception is java.io.FileNotFoundException: Could not open ServletContext resource

<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> 注意这段配置尽量写在靠前的位置,因为在tomcat启动后web项目会先初始化上下文,我们这段配置正是指定了上下文配置的文件位置,在上下文初始化完成之后才应当继

?--Porg.springframework.beans.MethodInvocationException: Property &#39;username&#39; threw exception; nested exception is java.lang.NullPointerException

使用BoneCP作为连接池,在启动Tomcat报出以下异常: 一月 02, 2016 2:12:17 下午 org.apache.tomcat.util.digester.SetPropertiesRule begin警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:mynewdpi' did not find a

nested exception is java.sql.SQLException: Incorrect string value: &#39;\xE7\x99\xBB\xE9\x99\x86...&#39; for column &#39;image&#39; at row 1

HTTP Status 500 - Hibernate operation: could not insert: [cn.itcast.shop.product.vo.Product]; uncategorized SQLException for SQL [insert into product (pname, market_price, shop_price, image, pdesc, is_hot, pdate, csid) values (?, ?, ?, ?, ?, ?, ?, ?)

spring quartz:Instantiation of bean failed; nested exception is java.lang.IncompatibleClassChangeErr

spring 调度类初始化失败,分析是jar包冲突,日志如下: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scheduler' defined in file [D:\InstallApplication\apache-tomcat-7.0.23\wtpwebapps\campaign\WEB-INF\classes\spring\applicationConte