If you define a field as static, then there is only one such field per class. In contrast, each object has its own copy of all instance fields. For example:

class Employee
{
private static int nextID = 1; private int id;
...
} public void setId()
{
id = nextId;
nextId++;
}

Static methods are methods that do not operate on objects. For example, the pow method of the Math class is a static method. The expression Math.pow(x,a) computes the power. It does not user any Math object to carry out its task. In other words, **it has no implicit parameter*. You can think of static methods as methods that don't have a this parameter.

A static method of the Employee class cannot access the id instance field because it does not operate on an object. However, a static method can access a static field. For example:

public static int getNextId()
{
return nextId; //returns static field
}

To call this method, you supply the name of the class:

int n = Employee.getNextid();

You would need to have an object reference of type Employee to invoke the method if you omit the keyword static.

Static Fields and Methods的更多相关文章

  1. Core Java Volume I — 4.4. Static Fields and Methods

    4.4. Static Fields and MethodsIn all sample programs that you have seen, the main method is tagged w ...

  2. Android JNI 学习(九):Static Fields Api & Static Methods Api

    一.Accessing Static Fields(访问静态域) 1. GetStaticFieldID jfieldIDGetStaticFieldID(JNIEnv *env, jclass cl ...

  3. PMD - Avoid autogenerated methods to access private fields and methods of inner / outer classes

    PMD错误 Avoid autogenerated methods to access private fields and methods of inner / outer classes 样例 p ...

  4. Resource annotation is not supported on static fields

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'paramUtil' d ...

  5. .NET并行计算和并发4-Thread-Relative Static Fields and Data Slots

    Thread Local Storage: Thread-Relative Static Fields and Data Slots 文章摘自msdn library官方文档 可以使用托管线程本地存储 ...

  6. Difference Between static and default methods in interface

    I was learning through interfaces when I noticed that you can now define static and default methods ...

  7. 【Java基础】Java反射——Private Fields and Methods

    Despite the common belief it is actually possible to access private fields and methods of other clas ...

  8. static 静态域 类域 静态方法 工厂方法 he use of the static keyword to create fields and methods that belong to the class, rather than to an instance of the class 非访问修饰符

    总结: 1.无论一个类实例化多少对象,它的静态变量只有一份拷贝: 静态域属于类,而非由类构造的实例化的对象,所有类的实例对象共享静态域. class Employee { private static ...

  9. Static and Instance Methods in JavaScript

    class.method/instance method https://abdulapopoola.com/2013/03/30/static-and-instance-methods-in-jav ...

随机推荐

  1. 台哥原创:java 数独源码

    2010年,当时正在做手机游戏的客户端开发工作. 每天加班之余,用了两三个晚上,开发了这个数独. 主要是生成数独数组的算法,有点难度.. ​ 如下图:点选数字栏里的数字后,界面上所有该数字会高亮显示. ...

  2. redirect thread aborted

    Why Response.Redirect causes System.Threading.ThreadAbortException? The correct pattern is to call t ...

  3. HttpClient配置及示例代码

    HttpComponents是Apache 旗下的项目.其中有一个HttpClient,即HTTP客户端. ... ... 大多时候我们只需要HttpClient,httpCore是开发服务端的我们可 ...

  4. Oracle-创建索引分区

    对大数据量索引进行分区同样能够优化应用系统的性能.一般来说,如果索引所对应的表的数据量非常大,比如几百万甚至上千万条数据,则索引也会占用很大的空间,这时,建议对索引进行分区. Oracle索引分区分为 ...

  5. 【ABAP系列】SAP ABAP 给初学者-解读function函数的export和import等参数

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP 给初学者-解读 ...

  6. CVTRES : fatal error CVT1100: duplicate resource

    升级某些VC6工程到VS2017,除了目录问题外,就是这个. 解决方法: Properties > Linker > Manifest File 第一项,Generate Manifest ...

  7. 关于VMWare的几种网络模式

    具体的可以参考这个博文:http://zhenyaliu.blog.163.com/blog/static/2377571920103775447527/

  8. C++中使用CMake编译管理项目

    CMake是一个跨平台的Makefile生成工具,可以根据特定的规则生成相应的Makefile文件,并对C/C++源代码进行编译和管理. 有一篇博客介绍CMake的使用,比较通俗易懂,链接地址是:Cm ...

  9. Android 中三种启用线程的方法

    在多线程编程这块,我们经常要使用Handler(处理),Thread(线程)和Runnable这三个类,那么他们之间的关系你是否弄清楚了呢? 首先说明Android的CPU分配的最小单元是线程,Han ...

  10. CSS的置换和非置换元素

    一个来自面试的坑. 面试的时候考官先问了行内元素和块级元素的区别,这个不难理解.然后一脚就踩进了,置换元素的坑.例如img就是行内置换元素,这种行内元素是可以设置宽高的. 什么是置换元素 一个内容不受 ...