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. 攻防世界 | level2

    # ! /usr/bin/env python # -*- coding:utf-8 -*- from pwn import * context.log_level = 'debug' elf = E ...

  2. php heredoc的用法详解

    Heredoc技术,在正规的PHP文档中和技术书籍中一般没有详细讲述,只是提到了这是一种Perl风格的字符串输出技术.但是现在的一些论坛程序,和部分文章系统,都巧妙的使用heredoc技术,来部分的实 ...

  3. 获取系统的documents路径

    获取路径 https://superuser.com/questions/1132288/windows-command-prompt-get-relocated-users-documents-fo ...

  4. Docker 里面新建mysql 容器

    1.获取MySQL镜像, a.直接从docker hub 下载docker镜像 docker pull +镜像名称 b.从别的项目上把镜像export出来 dockr load  i + 镜像的TAR ...

  5. ST表——————一失足成千古恨系列2

    在此先祝自己这个系列写的越少越好qwq(保证不超过4篇(flag已立)) 考试原题:(绝壁是看完复联出的) 第一反应:线段树??不对,是st表.嗯,没错.哎,st表咋写来着??完了凉了. 结果:写暴搜 ...

  6. STM32 I2C 难点---这个不错,留着慢慢研究

    来自:http://bbs.ednchina.com/BLOG_ARTICLE_2154168.HTM I2C 总线在所有嵌入式系统中用得极广, 是一个工业级别的总线, 但由于STM32 是一个32位 ...

  7. Mybatis入门(附源码压缩包下载)

    首先,来个项目全景预览,文章尾部附上Demo下载链接 [1]pom.xml配置(加入jar包) <project xmlns="http://maven.apache.org/POM/ ...

  8. 使用eclipse制作war包方法 web项目打包到tomcat

    打开eclipse在左侧右击项目名选择“Export”   在导出画面点击 “Web”->“WAR file”点击“Next”   点击“Browse…”选择文件的导出位置,Target run ...

  9. 使用 GitLab 的 OAuth2 认证服务

    原文地址 本文档讲述如何使用 GitLab 作为 OAuth 认证服务提供商,以通过 GitLab 的 OAuth 认证登录其他服务(例如持续集成工具 Drone). 如果想使用其他 OAuth 身份 ...

  10. Vagrant 入门 - 网络

    原文地址 现在,我们启动了 web 服务器,并且通过同步目录使用宿主机上的文件提供服务.然而,还只能通过虚拟机中的终端访问服务器.这一章节中,我们会使用 Vagrant 的网络特性,配置 Vagran ...