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. Linux(RHEL7)下安装vsftp服务

    1.安装vsftp(没有配置yum源的先配置yum源) yum install -y vsftpd 2.启动ftp服务 systemctl start vsftpd.service 3.打开防火墙 f ...

  2. python web自动化测试框架搭建(功能&接口)——接口用例实现

    测试用例基类: # coding=utf-8 import unittest import Logger log = Logger.Loger() class BaseCase(unittest.Te ...

  3. Convolutional Neural Networks(3):Convolution and Channels

    在CNN(1)和CNN(2)两篇文章中,主要说明的是CNN的基本架构和权值共享(Weight Sharing),本文则重点介绍卷积的部分. 首先,在卷积之前,我们的数据是4D的tensor(width ...

  4. 【python】 判断纯ascii串

    参考:http://stackoverflow.com/questions/3636928/test-if-a-python-string-is-printable print all(ord(c)& ...

  5. Visual Studio中把文件夹导入工程中

    VS用到的功能还是太少,记录备忘. 有的时候需要把其他库的源码导入当前工程直接使用,而这个库是源码形式,又带很多目录的. 之前从没遇到过这种情况,自己的库目录自己新建,添加. 第三方库一般有单独的Pr ...

  6. (转载)STL map与Boost unordered_map的比较

    原链接:传送门 今天看到 boost::unordered_map,它与 stl::map的区别就是,stl::map是按照operator<比较判断元素是否相同,以及比较元素的大小,然后选择合 ...

  7. mysql 数据库表结构对比语句

    判断两个数据库互相不存在的表 select a.TABLE_SCHEMA,a.TABLE_NAME from information_schema.TABLES a where a.TABLE_SCH ...

  8. PHP_CodeIgniter Github实现个人空间

    github支持github Pages 可以实现自己的个人空间 XXX.github.io/project 1 注册自己的github账户 2 需要设置自己的user_name, user_name ...

  9. C#设计模式:工厂模式

    一,工厂模式 using System; using System.Collections.Generic; using System.Linq; using System.Text; using S ...

  10. cronsun任务管理器部署文档

    一.cronsun介绍 1)cronsun产生的背景 大量的 crontab 任务散布在各台服务器,带来了很高的维护成本 任务没有按时执行,甚至失败了很久才发现,需要重试或排查 crontab 分散在 ...