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的更多相关文章

  1. 【java】 field 和 variable 区别及相关术语解释

    Having said that, the remainder of this tutorial uses the following general guidelines when discussi ...

  2. 关于Java中的static关键字

    Java中的 static 关键字,确实是一个关键的字(key word),今天就来总结一下它的用法,说说为什么关键. Java中的 static 关键字主要是用来做内存管理的.理解了这句话才能够比较 ...

  3. (转)Java中的static关键字解析

    转载: http://www.cnblogs.com/dolphin0520/p/3799052.html 一.static关键字的用途 在<Java编程思想>P86页有这样一段话: &q ...

  4. Java中的static关键字解析

    Java中的static关键字解析 static关键字是很多朋友在编写代码和阅读代码时碰到的比较难以理解的一个关键字,也是各大公司的面试官喜欢在面试时问到的知识点之一.下面就先讲述一下static关键 ...

  5. java中的static使用--静态变量、静态方法

    Java 中的 static 使用之静态变量 大家都知道,我们可以基于一个类创建多个该类的对象,每个对象都拥有自己的成员,互相独立.然而在某些时候,我们更希望该类所有的对象共享同一个成员.此时就是 s ...

  6. (转)Java中的static关键字解析

    转自http://www.cnblogs.com/dolphin0520/p/3799052.html 一.static关键字的用途 在<Java编程思想>P86页有这样一段话: “sta ...

  7. MySQL: @variable vs. variable. Whats the difference?

    MySQL: @variable vs. variable. Whats the difference?   up vote351down votefavorite 121 In another qu ...

  8. Java中的static的使用

    Java中的static使用之静态变量 神话丿小王子的博客主页 1.Java 中被static修饰的成员称为静态成员或类成员.它属于整个类所有,而不是某个对象所有,即被类的所有对象所共享.且优先于对象 ...

  9. java中的static详解

    如果一个类成员被声明为static,它就能够在类的任何对象创建之前被访问,而不必引用任何对象.static 成员的最常见的例子是main( ) .因为在程序开始执行时必须调用main() ,所以它被声 ...

随机推荐

  1. git 用Gitk /usr/bin/which: no wish

    /usr/bin/which: no wish 安装yum -y install tcl 和yum -y install tk 显示所有的分支 $gitk --all 显示所有的分支 $gitk -- ...

  2. jsp中如何取得当前页面完整的URL

    JSP页面 <% String url = request.getScheme()+"://"+ request.getServerName()+request.getReq ...

  3. Highcharts 本地导出图片 Java

    下载的Highcharts-2.3.5.zip 解压后 有 E:\Highcharts\Highcharts-2.3.5\exporting-server\java 目录 提供了Java实现的导出应用 ...

  4. jquery-mobile表单提交问题

    关于使用jquery-mobile表单提交遇到的问题     当你使用了jquery-mobile的时候,如果你在前台提交一个了一个form表单,而在后台你处理完业务逻辑之后想要重定向到另一个方法或页 ...

  5. 编写一个名为Test的主类,类中只有一个主方法; 在主方法中定义一个大小为50的一维整型数组,数组名为x,数组中存放着{1, 3,5,…,99}输出这个数组中的所有元素,每输出十个换一行;在主方法中定义一 个大小为10*10的二维字符型数组,数组名为y,正反对角线上存的是‘*’,其余 位置存的是‘#’;输出这个数组中的所有元素。

    package liu0915; import java.util.Random; public class Test0915sz { public static void main(String[] ...

  6. C#常用功能函数小结(.NET 4.5)

    今天有空,把C#常用的功能总结一下,希望对您有用.(适用于.NET Framework 4.5) 1. 把类转换为字符串(序列化为XML字符串,支持xml的namespace) using Syste ...

  7. 【转载】jQuery插件开发精品教程,让你的jQuery提升一个台阶

    要说jQuery 最成功的地方,我认为是它的可扩展性吸引了众多开发者为其开发插件,从而建立起了一个生态系统.这好比大公司们争相做平台一样,得平台者得天下.苹果,微软,谷歌等巨头,都有各自的平台及生态圈 ...

  8. SwfUpload学习记录

    参考资料: SWFUpload 2.5.0版 官方说明文档 中文翻译版 了解SWFUpload 多文件上传配置详解 WEB版一次选择多个文件进行批量上传(swfupload)的解决方案 jQuery轻 ...

  9. Javascript 笔记与总结(2-11)暴力操作节点

    innerHTML 代表节点内的内容,能读能写 虽然不是 W3C 规定的标准,但是各浏览器都支持得很好 [例] <!DOCTYPE html> <html lang="en ...

  10. 理解 Python 中的 *args 和 **kwargs

    Python是支持可变参数的,最简单的方法莫过于使用默认参数,例如: def test_defargs(one, two = 2): print 'Required argument: ', one ...