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. git cherry-pick的使用

    [Git] Git整理(五) git cherry-pick的使用 2018年07月13日 23:49:16 FightFightFight 阅读数:31649    版权声明:本文为博主原创文章,未 ...

  2. QT5 QTreeView添加右键菜单

    C++ QT5学习--QTreeView控件创建右键菜单 QTreeView是QWidget的子类,我们再改写QTreeView类的时候,注意的是继承关系. 1.TreeView.h class Tr ...

  3. 【CDN+】 Hbase入门 以及Hbase shell基础命令

    前言 大数据的基础离不开Hbase, 本文就hbase的基础概念,特点,以及框架进行简介, 实际操作种需要注意hbase shell的使用. Hbase  基础 官网:https://hbase.ap ...

  4. sql查询XML

    --查询Extra里节点UName值等于“黄”的所有信息 select * from t_UserPayLog where Extra.exist('//UName[.="黄"]' ...

  5. css样式表的理解

    全拼Cascading Style Sheete 美化html网页 1分为 内联样式表 和html联合显示 内嵌样式表 在单独区域内嵌,必须在head 外部样式表 需建一个css文件,保存并附加 2选 ...

  6. 批量执行SQL脚本

    新建文件夹all_sql,并将需要执行的sql脚本放入其中. 新建bat脚本,执行即可,ORACLE 也可改Mysql,按需:如下 ::echo off :: @echo off echo 开始执行数 ...

  7. ElasticSearch2.2.0安装

    一.ElasticSearch2.2.0安装 1.下载ElasticSearch2.2.0安装包 https://download.elastic.co/elasticsearch/elasticse ...

  8. 使用 Vagrant 搭建 Kubernetes 本地测试环境

    Vagrant 中文资料 参考资料 Kubernetes 需要一个至少包含三个节点的分布式系统.如果想学习 Kubernetes,或只是在本地搭建测试环境,则可以通过 Vagrant 来简单的实现. ...

  9. hdu6575Budget

    Problem Description Avin’s company has many ongoing projects with different budgets. His company rec ...

  10. iframe父页面和子页面高度自适应

    父页HTML: <iframe  id="mainframe" name="mainframe"  style="width:100%;&quo ...