Java: Class Variable/Static Variable
1. Class Variable/Static Variable:
- Class variable is also known as static variable with "static" keyword inside the class but outside the methods.
- There is only one copy of class variable is no matter how many objects are initiated from this class.
- Class variable is accessed as: className.classVariableName.
- Static variable is pretty like constant, declared with key word "static", stored in static memory, created when program begins and destroyed when program ends.
2. Java Static Method:
- A static method belongs to a class rather than a object.
- A static method could be invoked without creating an object.
- Static method could access and change static data value.
3. Static Block:
- It is used to initialize static data member.
- It is executed before main method at the time of class loading.
4. Static Class:
- There is inner class nested in outer class, inner class could be static class, out class can't be static class.
- Inner static class doesn't need reference of outer class, but inner non-static class need reference of outer class.
- Inner static class could only access outer static members of outer class.
class OuterClass{
private static String msg = "GeeksForGeeks"; // Static nested class
public static class NestedStaticClass{ // Only static members of Outer class is directly accessible in nested
// static class
public void printMessage() { // Try making 'message' a non-static variable, there will be
// compiler error
System.out.println("Message from nested static class: " + msg);
}
} // non-static nested class - also called Inner class
public class InnerClass{ // Both static and non-static members of Outer class are accessible in
// this Inner class
public void display(){
System.out.println("Message from non-static nested class: "+ msg);
}
}
}
class Main
{
// How to create instance of static and non static nested class?
public static void main(String args[]){ // create instance of nested Static class
OuterClass.NestedStaticClass printer = new OuterClass.NestedStaticClass(); // call non static method of nested static class
printer.printMessage(); // In order to create instance of Inner class we need an Outer class
// instance. Let us create Outer class instance for creating
// non-static nested class
OuterClass outer = new OuterClass();
OuterClass.InnerClass inner = outer.new InnerClass(); // calling non-static method of Inner class
inner.display(); // we can also combine above steps in one step to create instance of
// Inner class
OuterClass.InnerClass innerObject = new OuterClass().new InnerClass(); // similarly we can now call Inner class method
innerObject.display();
}
}
Java: Class Variable/Static Variable的更多相关文章
- 【java】 field 和 variable 区别及相关术语解释
Having said that, the remainder of this tutorial uses the following general guidelines when discussi ...
- 关于Java中的static关键字
Java中的 static 关键字,确实是一个关键的字(key word),今天就来总结一下它的用法,说说为什么关键. Java中的 static 关键字主要是用来做内存管理的.理解了这句话才能够比较 ...
- (转)Java中的static关键字解析
转载: http://www.cnblogs.com/dolphin0520/p/3799052.html 一.static关键字的用途 在<Java编程思想>P86页有这样一段话: &q ...
- Java中的static关键字解析
Java中的static关键字解析 static关键字是很多朋友在编写代码和阅读代码时碰到的比较难以理解的一个关键字,也是各大公司的面试官喜欢在面试时问到的知识点之一.下面就先讲述一下static关键 ...
- java中的static使用--静态变量、静态方法
Java 中的 static 使用之静态变量 大家都知道,我们可以基于一个类创建多个该类的对象,每个对象都拥有自己的成员,互相独立.然而在某些时候,我们更希望该类所有的对象共享同一个成员.此时就是 s ...
- (转)Java中的static关键字解析
转自http://www.cnblogs.com/dolphin0520/p/3799052.html 一.static关键字的用途 在<Java编程思想>P86页有这样一段话: “sta ...
- MySQL: @variable vs. variable. Whats the difference?
MySQL: @variable vs. variable. Whats the difference? up vote351down votefavorite 121 In another qu ...
- Java中的static的使用
Java中的static使用之静态变量 神话丿小王子的博客主页 1.Java 中被static修饰的成员称为静态成员或类成员.它属于整个类所有,而不是某个对象所有,即被类的所有对象所共享.且优先于对象 ...
- java中的static详解
如果一个类成员被声明为static,它就能够在类的任何对象创建之前被访问,而不必引用任何对象.static 成员的最常见的例子是main( ) .因为在程序开始执行时必须调用main() ,所以它被声 ...
随机推荐
- SPIE Example References
Journal Article[1] Davis, A. R., Bush, C., Harvey, J. C. and Foley, M. F., "Fresnel lenses in r ...
- 大数据技术hadoop入门理论系列之二—HDFS架构简介
HDFS简单介绍 HDFS全称是Hadoop Distribute File System,是一个能运行在普通商用硬件上的分布式文件系统. 与其他分布式文件系统显著不同的特点是: HDFS是一个高容错 ...
- 新机上岗 Core i7-4790 @ 3.60GHz 四核 / 16 GB ( 金士顿 DDR3 1866MHz ) / GeForce GTX 970 ( 4 GB / 七彩虹 )
新机上岗 ==============================电脑型号 华硕 All Series 台式电脑操作系统 Windows 7 旗舰版 64位 SP1 ( DirectX 11 ) ...
- 通过get方式传递参数
就一个图吧
- Solr学习笔记之2、集成IK中文分词器
Solr学习笔记之2.集成IK中文分词器 一.下载IK中文分词器 IK中文分词器 此文IK版本:IK Analyer 2012-FF hotfix 1 完整分发包 二.在Solr中集成IK中文分词器 ...
- 《你不知道的JavaScript》读书笔记(一)作用域
名词 引擎:从头到尾负责整个 JavaScript 程序的 编译 及 执行 过程. 编译器:负责 语法分析 及 代码生成. 作用域:负责收集并维护由所有声明的标识符(变量)组成的一系列查询,并实施一套 ...
- Redis 笔记与总结3 list 类型
redis 版本 [root@localhost ~]# redis-server --version Redis server v= sha=: malloc=jemalloc- bits= bui ...
- 关于DWZ模板中全选的使用
只在使用DWZ框架时有用 模板中 <input type="checkbox" name="rule_id[]" />选项1 <input t ...
- 初步理解Java的三大特性——封装、继承和多态
声明:整理自网络,如有雷同,请联系博主处理 一.封装 封装从字面上来理解就是包装的意思,专业点就是信息隐藏,是指利用抽象数据类型将数据和基于数据的操作封装在一起,使其构成一个不可分割的独立实体,数据被 ...
- ORACLE FormBuilder触发器执行顺序
1.当打开FORM时: (1)PRE-FORM (2)PRE-BLOCK(BLOCK级) (3)WHEN-NEW-FORM-INSTANCE (4)WHEN-NEW-BLOCK-INSTANCE (5 ...