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 ...
随机推荐
- CSU 1551 Longest Increasing Subsequence Again(树状数组 或者 LIS变形)
题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1551 升级版:Uva 1471 题意: 让你求删除一段连续的子序列之后的LIS. 题 ...
- js/jq 小功能函数
1.点击复制内容到剪贴板 function copyToClip(str) { var save = function(e) { e.clipboardData.setData('text/plain ...
- noi.ac #712 练级
分析 把船当作点 练级当作边 发现一个连通块大于n-1的边的条数的奇偶性影响这个连通块的答案 于是并查集维护即可 代码 #include<bits/stdc++.h> using name ...
- P1063能量项链
传送 这又是一道经典的区间DP题. 复习一下区间DP的做法. 三重循环,第一层枚举区间长度,第二层枚举起点,第三层枚举断点. 区间长度是从1到n-1(因为如果是从1到n的话,1+n≠n,所以是1到n- ...
- Vagrant 手册之 Vagrantfile - 配置版本
原文地址 配置版本是 Vagrant 1.1+(引入了大量新功能和配置选项) 能够与 Vagrant 1.0.x Vagrantfiles 保持向后兼容的机制. 现在运行 vagrant init 时 ...
- Vue混入:全局混入
一 项目结构 二 main.js import Vue from "vue"; import App from "./App.vue"; Vue.config. ...
- Bootstrap 学习笔记13 附加导航插件
附加导航代码: <style> a:focus { outline: none; } .nav-pills { width: 150px; } .nav-pills.affix { top ...
- C#将数据写入本地文件
在平时开发过程中,可能会碰到内网测试没问题,但是更新到外网时会报错,这时我们又无法在外网进行调试.如果我们分析完业务可能产生的问题还是无法得到报错的原因,那么可以在关键的地方加上异常处理,然后将异常或 ...
- Spring事务传播及数据库事务操作
从Spring 事务配置说起 先看看Spring 事务的基础配置 <aop:aspectj-autoproxy proxy-target-class="true"/> ...
- Runnable、Callable、Future、FutureTask的区别
转自:https://blog.csdn.net/jdsjlzx/article/details/52912701 FutureTask既是Future.Runnable,又是包装了Callable( ...