`

java - static class explained

    博客分类:
  • java
阅读更多

If you are from a C# background, you will know what I am talking about. In c# when a class is static, that means you cannot instantiate instance from this class. and beside, you cannot have instance method in the class definition. 

 

however, static class in java is a bit different, the static class is used in java as nested class, there is no such outer static class in java. 

 

so static class is in constrast to non-static nested class, where there is certain boundage from the nested class to its containing class. we will see an example below to show their difference. 

 

/** 
 * 
 * @author boqwang
 *
 * a note on the static class in Java
 * - C# interprets static classes as abstract final
 * - java class can be either abstract or final, not both. To Prevent a class from being instantiated, one can declare a private constructor.
 */


class OuterClass {
  public static class StaticNestedClass {
	  
  }
  
  public class InnerClass { 
	  
  }
  
  public InnerClass getAnInnerClass () { 
	  return new InnerClass();
	  // which is equivalent to 
	  // return this.new InnerClass();
  }
  
  public static StaticNestedClass getAnClassStatically() {
	  return new StaticNestedClass();
  }
  
  public static StaticNestedClass getAnInnerClassStatically() {
	  // this won't work, the instance to the OuterClass is needed.
	  // return new InnerClass();
	  return null;
  }
  
	
}


class OtherClass {
	// use of a static nested clas; - a static class can be used nested to an outer class, and which does not depends on the instance of the containing class.
	private OuterClass.StaticNestedClass staticNestedClass = new OuterClass.StaticNestedClass();
	
	// this won't work,  you will need an instance of the outer class to make it work 
	// private OuterClass.InnerClass innerClass = new OuterClass.InnerClass(); 
	
	// use of inner class
	private OuterClass outerclass = new OuterClass();
	private OuterClass.InnerClass innerClass2 = outerclass.getAnInnerClass();
	
	// or you can do this way to explicitly create the Inner class instance 
	private OuterClass.InnerClass innerClass3 = outerclass.new InnerClass();
	
}

 

As you can see, 

 

  • C# interprets static classes as abstract final
  • Java can be either abstract or final, not both. To Prevent a class from being instantiated, one can declare a private constructor.

分享到:
评论

相关推荐

    设计模式英文版-Design Pattern Explained

    在《设计模式英文版-Design Pattern Explained》这本书中,作者深入浅出地介绍了设计模式的概念及其在软件开发中的应用。本书不仅适合已经熟悉面向对象编程的开发者,也适合那些希望通过学习设计模式来提高自己技术...

    vc编程系列之Addison Wesley - Design Patterns Explained

    《VC编程系列之Addison Wesley - Design Patterns Explained》是一本深入探讨面向对象设计模式的著作,主要针对C++开发者,旨在提升他们在软件设计中的能力。设计模式是软件工程中的重要概念,它提供了在特定场景下...

    Pacemaker-2.0-Pacemaker_Explained-en-US.pdf

    文档《Pacemaker 2.0 Configuration Explained》是一个指南,为读者提供了Pacemaker配置选项的完整介绍。这份文件以字母顺序(A-Z)的方式,详细讲解了Pacemaker配置中从A到Z的所有相关选项,并且以一种条理清晰的...

    HTTP - Http3 explained

    #### 引言 ... #### 为何QUIC?...QUIC是一种由Google开发的传输层协议,用于在客户端和服务器之间提供低延迟和高带宽的数据传输。与传统的TCP/IP协议相比,QUIC通过以下方式改进了网络通信: ...1. **消除头部阻塞**:在...

    Neural Networks and Deep Learning - Deep Learning explained to your granny

    Neural Networks & Deep Learning Deep Learning explained to your granny – A visual introduction for beginners who want to make their own Deep Learning Neural Network By Pat Nakamoto 神经网络与深度学习...

    Addison Wesley - LDAP Directories Explained.chm

    LDAP编程手册, 本书详细的论述了如何使用使用LDAP, 从C语言和JAVA做为例子来详细的解说LDAP编程。

    Addison Wesley - Symbian OS Explained Effective C++ Programming for Smartphones.pdf

    ### Symbian OS Explained: Effective C++ Programming for Smartphones #### 核心知识点概览 本书《Symbian OS Explained: Effective C++ Programming for Smartphones》由Jo Stichbury编写,是一本针对Symbian...

    Addison Wesley - Symbian OS Explained Effective C++ Programming for Smartphones(解密:iTePub.Net).zip

    1 Class Name Conventions on Symbian OS 1 1.1 Fundamental Types 1 1.2 T Classes 3 1.3 C Classes 4 1.4 R Classes 6 1.5 M Classes 7 1.6 Static Classes 11 1.7 Buyer Beware 11 1.8 Summary 12 2 Leaves: ...

    开放XML-标记说明Open XML - The Markup Explained

    详细介绍了Open XML中的三种主要标记语言,这是Microsoft Office 2007平台中实现的许多新技术之一。

    Awk one-liners explained 中文版

    ### Awk One-Liners Explained (Chinese Edition) #### 第一部分:行距、编号与运算 在探讨Awk的使用技巧之前,我们先简单了解一下Awk的基本概念。Awk是一种强大的文本处理工具,常用于Linux和Unix环境中。它可以...

    Naive-bayes-explained:这是python中朴素贝叶斯wrt实现的非常深入的解释,可以在机器学习应用程序中使用

    **朴素贝叶斯分类器详解** 朴素贝叶斯(Naive Bayes)是一种基于概率的机器学习算法,它源于贝叶斯定理,并且“朴素”一词来源于它对特征之间独立性的假设。在实际应用中,朴素贝叶斯模型因其简单、高效以及在处理...

    Deep-Reinforcement-Learning-Explained

    在这个项目中,“Deep-Reinforcement-Learning-Explained”旨在深入浅出地介绍DRL的基本概念、算法以及实际应用。 首先,我们来理解深度学习。深度学习是一种机器学习方法,通过多层神经网络来学习数据的抽象表示。...

    javascript-concepts-explained

    "javascript-concepts-explained" 涵盖了一系列JavaScript的核心概念,旨在帮助开发者深入理解这门语言的精髓。在这个压缩包文件中,我们可以期待找到一系列关于JavaScript基本原理、语法和高级特性的解释。 首先,...

    diffie-hellman-explained:Diffie Hellman 密钥交换说明

    #Diffie-Hellman 解释了关于应用密码学的闪电演讲,特别是使用 Node... ##跑步 git clone https://github.com/nickdesaulniers/diffie-hellman-explained.gitcd diffie-hellman-explainednpm installnode tutorial.js

    Multivariate-Statistical-Techniques-Explained

    本资源“Multivariate-Statistical-Techniques-Explained”聚焦于通过R语言来演示和理解这些技术,帮助用户以可视化的方式深入学习。以下是涵盖的几个核心知识点: 1. 距离测度:在多元数据中,测量两个样本或观测...

    web-development-explained-to-myself:这个仓库是我的橡皮鸭

    这个名为"web-development-explained-to-myself"的仓库就像是一个个人的学习笔记,旨在深入理解Web开发的各个方面。让我们一起探索这个领域的核心概念。 前端开发是用户在浏览器中看到和互动的部分。它包括HTML(超...

    Open+XML+Explained.pdf

    ### Open XML Explained #### 一、概述与目标读者 《Open XML Explained》是一本深入探讨Microsoft Office Open XML标准的书籍,旨在帮助开发者更好地理解并应用这一文档格式。本书适用于那些希望掌握如何使用Open...

    Fortran 90,95 Explained (2nd edition) - Metcalf (1999)

    需要Fortran 90,95编程的经典参考。

    pex-exp-deferred-rendering-explained:延迟渲染博客文章代码

    `pex-exp-deferred-rendering-explained-master`这个压缩包可能包含了一个示例项目,展示了如何使用Pex库来创建延迟渲染的WebGL应用。通过研究这个项目,你可以了解如何配置帧缓冲、设置着色器、管理纹理,以及如何...

Global site tag (gtag.js) - Google Analytics