博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java静态类_Java静态类
阅读量:2531 次
发布时间:2019-05-11

本文共 2472 字,大约阅读时间需要 8 分钟。

java静态类

Today we will look into java static class. It’s a good interview question to test your knowledge about nested classes in java.

今天,我们将研究java静态类。 测试您对Java嵌套类的了解是一个很好的面试问题。

Java doesn’t allow top level static classes, for example if we try to make a class static like below.

Java不允许顶层静态类,例如,如果我们尝试使类像下面这样静态化,则是不允许的。

Test.java

Test.java

static class Test {}

We get following compilation error.

我们得到以下编译错误。

$ javac Test.java Test.java:1: error: modifier static not allowed herestatic class Test {       ^1 error

Java静态类 (Java static class)

So is it possible to have static class in java?

那么有可能在Java中有静态类吗?

Yes, java supports nested classes and they can be static. These static classes are also called static nested classes.

是的,java支持嵌套类,它们可以是静态的。 这些静态类也称为静态嵌套类。

Java static nested class can access only static members of the outer class. Static nested class behaves similar to top-level class and is nested for only packaging convenience.

Java静态嵌套类只能访问外部类的静态成员。 静态嵌套类的行为类似于顶级类,并且仅出于包装方便而嵌套。

An instance of static nested class can be created like below.

静态嵌套类的实例可以如下创建。

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

Java静态类示例 (Java static class example)

Let’s look at the example of java static class and see how we can use it in java program.

让我们看一下Java静态类的示例,看看如何在Java程序中使用它。

package com.journaldev.java.examples;public class OuterClass {	private static String name = "OuterClass";	// static nested class	static class StaticNestedClass {		private int a;		public int getA() {			return this.a;		}		public String getName() {			return name;		}	}}

Now let’s see how to instantiate and use static nested class.

现在让我们看看如何实例化和使用静态嵌套类。

package com.journaldev.java.examples;public class StaticNestedClassTest {	public static void main(String[] args) {				//creating instance of static nested class		OuterClass.StaticNestedClass nested = new OuterClass.StaticNestedClass();				//accessing outer class static member		System.out.println(nested.getName()); 	}}

Java静态类文件 (Java static class file)

Java static class file is named as OuterClass$StaticClass.class. Below image shows this for our example program.

Java静态类文件名为OuterClass$StaticClass.class 。 下图显示了我们的示例程序。

Java静态类的好处 (Java static class benefits)

The only benefit I could think of is encapsulation. If the static nested class works only with the outer class then we can keep nested class as static to keep them close.

我唯一想到的好处就是封装。 如果静态嵌套类仅与外部类一起使用,那么我们可以将嵌套类保持为静态,以使它们保持紧密状态。

翻译自:

java静态类

转载地址:http://gllzd.baihongyu.com/

你可能感兴趣的文章
UIDynamic(物理仿真)
查看>>
Windows下安装Redis
查看>>
迷宫实现
查看>>
【字符编码】Java字符编码详细解答及问题探讨
查看>>
学习操作系统导图
查看>>
在线的JSON formate工具
查看>>
winform非常实用的程序退出方法!!!!!(转自博客园)
查看>>
xml解析
查看>>
centos安装vim
查看>>
linux工作调度(计划任务)
查看>>
hdu--1698 Just a Hook(线段树+区间更新+懒惰标记)
查看>>
Python学习笔记-EXCEL操作
查看>>
二月二——头牙诗词
查看>>
《吴忠与富平》之四:汉三水属国(北地属国、安定属国)
查看>>
丁酉立秋喜逢风雨
查看>>
vim删除#开头的行和正则表达式笔记
查看>>
python3 提成计算
查看>>
VBA赋值给指定单元格
查看>>
抽象类和接口总结回顾
查看>>
【语言处理与Python】5.3使用Python字典映射词及其属性
查看>>