Static Fields and Methods
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的更多相关文章
- 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 ...
- Android JNI 学习(九):Static Fields Api & Static Methods Api
一.Accessing Static Fields(访问静态域) 1. GetStaticFieldID jfieldIDGetStaticFieldID(JNIEnv *env, jclass cl ...
- 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 ...
- Resource annotation is not supported on static fields
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'paramUtil' d ...
- .NET并行计算和并发4-Thread-Relative Static Fields and Data Slots
Thread Local Storage: Thread-Relative Static Fields and Data Slots 文章摘自msdn library官方文档 可以使用托管线程本地存储 ...
- 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 ...
- 【Java基础】Java反射——Private Fields and Methods
Despite the common belief it is actually possible to access private fields and methods of other clas ...
- 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 ...
- Static and Instance Methods in JavaScript
class.method/instance method https://abdulapopoola.com/2013/03/30/static-and-instance-methods-in-jav ...
随机推荐
- 牛客多校第五场G
subsequence 1 只要处理长度等于t的. 转移方程没想出来QAQ $dp(i,j,0)$代表到$s[i]$为止有多少个前缀序列与$t[0\cdots j]$相同 所以有$dp(i,j,0)= ...
- hadoop平台搭建
前言 这是小的第一次搭建hadoop平台,写下这篇博客有以下几个目的(ps:本博只记录在linux系统下搭建hadoop的步骤,如果需要了解在其他平台上搭建hadoop的步骤,还请移步): 1.希望大 ...
- 29 August
P1352 Bosses' Masquerade 树形DP模板. #include <cstdio> #include <algorithm> using namespace ...
- Newtonsoft.Json源码的solution打开之后,无法加载project
无法加载项目 https://github.com/JamesNK/Newtonsoft.Json C:\repository\GitHub\Other\Newtonsoft.Json\Src\New ...
- Python3实现简单的钉钉机器人调用
具体可以参考开发文档:https://ding-doc.dingtalk.com/doc#/serverapi3/iydd5h from urllib import parse, request im ...
- WEB安全----XSS和CSRF
随着Web2.0.社交网络.微博等等一系列新型的互联网产品的诞生,基于Web环境的互联网应用越来越广泛,企业信息化的过程中各种应用都架设在Web平台上,Web业务的迅速发展也引起黑客们的强烈关注,接踵 ...
- v-text、v-html、v-bind、v-show
<!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...
- 【MM系列】SAP KP26 报工出错
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP KP26 报工出错 前言 ...
- oracle中not in 和 in的代替用法
-- not in 的替代写法select col from table1 where col not in(select col from table2); select col,table2.co ...
- mysql 5.1.34
在make之前,将MAKEFILE中的do abi check注释,不要注释名字... mysql 5.1 编译安装 分类: mysql2012-04-06 13:01 17175人阅读 评论(0) ...