static修饰类的作用
Java里面static一般用来修饰成员变量或函数。但有一种特殊用法是用static修饰内部类,普通类是不允许声明为静态的,只有内部类才可以。被static修饰的内部类可以直接作为一个普通类来使用,而不需实例一个外部类(见如下代码)
Java代码
public class OuterClass {
public static class InnerClass{
InnerClass(){
System.out.println("============= 我是一个内部类'InnerClass' =============");
}
}
} Java代码
public class TestStaticClass {
public static void main(String[] args) {
// 不需要new一个OutClass
new OuterClass.InnerClass();
}
} 如果没有用static修饰InterClass,则只能按如下方式调用: Java代码
package inner_class; public class OuterClass {
public class InnerClass{
InnerClass(){
System.out.println("============= 我是一个内部类'InnerClass' =============");
}
}
} Java代码
public class TestStaticClass {
public static void main(String[] args) {
// OutClass需要先生成一个实例
OuterClass oc = new OuterClass();
oc.new InnerClass();
}
}
static修饰类的作用的更多相关文章
- 【c++基础】static修饰的函数作用与意义
static修饰的函数作用与意义 static修饰的函数叫做静态函数,静态函数有两种,根据其出现的地方来分类: 如果这个静态函数出现在类里,那么它是一个静态成员函数: 静态成员函数的作用在于:调用这个 ...
- Java中static修饰类的问题
Java中static修饰类的问题 众所周知,Java中static关键字可以修饰方法与变量: 修饰变量的时候,这个变量属于类变量,可以直接通过类名.变量名来引用. 修饰方法的时候可以直接通过类名.方 ...
- C# Static修饰符的作用
MSDN上的定义 Use the static modifier to declare a static member, which belongs to the type itself rather ...
- 转载----c++ static修饰的函数作用与意义
static修饰的函数叫做静态函数,静态函数有两种,根据其出现的地方来分类: 如果这个静态函数出现在类里,那么它是一个静态成员函数: 静态成员函数的作用在于:调用这个函数不会访问或者修改任何对象(非s ...
- static修饰的成员与非static修饰类的成员的区别
① 格式 : 1> static修饰的,称为静态成员,非static修饰的,称为非静态成员. ② 内存位置: 1>static修饰的,在方法区的静态区中,非static修饰的,在堆中的对象 ...
- C#中static修饰符的作用
static在C#中表示的是静态的,比如一个静态的字段是归类型所有,而非归对象所有,也就是说,在调用这个字段时,只能用类型去调,而不能用对象. 实例字段时随着对象创建而创建,对象销毁而销毁,而静态字段 ...
- Java final和static 修饰符
一.final final是不变的,最终的意思.可以用来修饰变量,方法,类. 1. 修饰变量 private final int a = 2; private final int b; // fina ...
- static关键字的作用(修饰类、方法、变量、静态块)
1. static修饰的类只能为内部类,普通类无法用static关键字修饰.static修饰的内部类相当于一个普通的类,访问方式为(new 外部类名.内部类的方法() ).如下所示: public c ...
- [转载]能不能同时用static和const修饰类的成员函数?
题目(一):我们可以用static修饰一个类的成员函数,也可以用const修饰类的成员函数(写在函数的最后表示不能修改成员变量,不是指写在前面表示返回值为常量).请问:能不能同时用static和con ...
随机推荐
- 为电脑添加u盘写保护
需求:解决在公共打印PC机上u盘病毒的传染,设置后该PC机将不能对u盘文件进行写操作 修改注册表,在HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Contro ...
- MongoDB 快速入门
本作品由Man_华创作,采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可.基于http://www.cnblogs.com/manhua/上的作品创作. MongoDB No ...
- 对datatable进行linq过滤
实例: DataTable data = new DataTable(); data.Columns.Add("schoolid", Type.GetType("Syst ...
- Jconsole 工具介绍和使用方法
http://chain.blog.163.com/blog/static/14084852320117934024410/ JConsole是一个基于JMX的GUI工具,JDK自带小工具 h ...
- Ubuntu12安装RobotFramework
安装Python Ubuntu默认已安装 安装pip wget https://bootstrap.pypa.io/get-pip.py python get-pip.pysudo apt-get i ...
- MySQL数据表导出某条记录
请按照步骤导出,否则可能会报错: ERROR (HY000): The MySQL server is running with the --secure-file-priv option so it ...
- Nginx与Apache的Rewrite规则的区别
一.Nginx Rewrite规则相关指令 Nginx Rewrite规则相关指令有if.rewrite.set.return.break等,其中rewrite是最关键的指令.一个简单的Nginx R ...
- git 工具 - 子模块(submodule)
From: https://git-scm.com/book/zh/v2/Git-%E5%B7%A5%E5%85%B7-%E5%AD%90%E6%A8%A1%E5%9D%97 子模块 有种情况我们经常 ...
- ios何时使用self.
本文转载至 http://blog.csdn.net/lvxiangan/article/details/27204265 何时使用self.在网上搜索或者论坛里的回复大多都是简简单单的说这与 ...
- cf-341C Iahub and Permutations
C. Iahub and Permutations time limit per test 1 second memory limit per test 256 megabytes input sta ...