Java关键字--static
在Java中,将关键字static分为三部分进行讨论,分别为Java静态变量、Java静态方法、Java静态类
Java Static Variables
- Java instance variables are given separate memory for storage. If there is a need for a variable to be common to all the objects of a single java class, then the static modifier should be used in the variable declaration.
- Any java object that belongs to that class can modify its static variables.
- Also, an instance is not a must to modify the static variable and it can be accessed using the java class directly.
- Static variables can be accessed by java instance methods also.
- When the value of a constant is known at compile time it is declared ‘final’ using the ‘static’ keyword.
(1)类的非静态变量会被每一个被实例化的对象分配一个内存空间,而静态变量会被类所有实例化的对象共享同一块内存空间。
(2)类的任何一个实例化对象对该静态变量的操作都会改变该静态变量的值
(3)可以直接通过 ClassName.static-Variable 对静态变量进行访问
(4)static 和final用来修饰成员变量和成员方法,可简单理解为“全局常量”。
Java Static Methods
- Similar to static variables, java static methods are also common to classes and not tied to a java instance.
- Good practice in java is that, static methods should be invoked with using the class name though it can be invoked using an object. ClassName.methodName(arguments) or objectName.methodName(arguments)
- General use for java static methods is to access static fields.
- Static methods can be accessed by java instance methods.
- Java static methods cannot access instance variables or instance methods directly.
- Java static methods cannot use the ‘this’ keyword.
(1)静态方法可以直接通过类名调用,任何的实例也都可以调用。
(2)静态方法中不能用this和super关键字,不能直接访问所属类的实例变量和实例方法(就是不带static的成员变量和成员成员方法),只能访问所属类的静态成员变量和成员方法 (这点和C++中是一致的)
(3)因为static方法独立于任何实例,因此static方法必须被实现,而不能是抽象的abstract。
Java Static Classes
- For java classes, only an inner class can be declared using the static modifier.
- For java a static inner class it does not mean that, all their members are static. These are called nested static classes in java
(1)只有内部类才能被声明为静态类
Java关键字--static的更多相关文章
- java关键字static使用总结
java关键字static使用总结 1.static修饰的方法被称之为静态方法也叫做类方法,加static的方法,可以通过类名直接访问,不加static的方法只能通过对象名访问. 静态方法可以直接通过 ...
- java笔记——Java关键字static、final使用小结
static 1. static变量 按照是否静态的对类成员变量进行分类可分两种:一种是被static修饰的变量,叫静态变量或类变量:另一种是没有被static修饰的变量,叫实例变量.两者的 ...
- Java关键字static、final
static 1. static变量 按照是否静态的对类成员变量进行分类可分两种:一种是被static修饰的变量,叫静态变量或类变量:另一种是没有被static修饰的变量,叫实例变量.两者的 ...
- Java关键字static、final使用小结
static 1. static变量 按照是否静态的对类成员变量进行分类可分两种:一种是被static修饰的变量,叫静态变量或类变量:另一种是没有被static修饰的变量,叫实例变量.两者的 ...
- [JAVA关键字] static & final
JAVA十分常见的关键字static & final 总的来说final表示常量,即不可更改的:而static表示静态,即全局的 1. final 类:不能被继承 2. final 方法:能被 ...
- Java 关键字 static
关键字static作用如下: 1. 为某个基本数据类型或对象分配单一的存储空间. 2. 实现某个属性或方法与类关联.在类被加载后类名可以直接调用静态成员方法(下面简称静态方法)或者访问静态成员变量(下 ...
- java 关键字static
在Java中static表示“全局”或者“静态”的意思,用来修饰成员变量和成员方法,当然也可以修饰代码块. Java把内存分为栈内存和堆内存, 栈内存用来存放一些基本类型的变量.数组和对象的引用, 堆 ...
- java关键字“static”
Java中static使用方法 1.static静态变量 静态变量:每个类只有一个,所有实例共享: 实例变量:每个实例只有一个: package test2; import java.lang.Str ...
- Java关键字——static
static申明属性 如果有属性希望被所有对象共享,则必须将其申明为static属性. 使用static声明属性,则此属性称为全局属性,有时候也称为静态属性. 当一个类的属性申明位static的时候, ...
随机推荐
- upgrade-php-5-1-to-php-5-3-using-yum-on-centos
wget -q -O - http://www.atomicorp.com/installers/atomic | shyum upgrade phpyum -y remove atomic-rele ...
- ansible 2.2的源码编译安装
ansible代码下载地址:http://releases.ansible.com/ansible/ # git clone git://github.com/ansible/ansible.git ...
- 【kd-tree】bzoj4154 [Ipsc2015]Generating Synergy
区间修改的kd-tree,打标记,下传. 每次询问的时候,从询问点向上找到根,然后依次下传下来,再回答询问. #include<cstdio> #include<algorithm& ...
- [Leetcode][JAVA] Triangled
Triangle Given a triangle, find the minimum path sum from top to bottom. Each step you may move to a ...
- 用c#开发微信 (21) 微信酒店预订系统
本系统主要是帮助酒店让客户可以通过微信预订房间,增加酒店的入住率. 1 微信里订酒店 1.1关注微信号 用微信扫描下面的二维码(微信号 webuscn),关注此微信号 1.2订房 点击微信号里的 微布 ...
- 从Mono 4.0观C# 6.0部分新特性
Struct的默认构造函数 struct Complex { public Int32 Re { get; set; } public Int32 Im { get; set; } public Co ...
- 分分钟用上C#中的委托和事件之窗体篇
上次以鸿门宴的例子写了一篇名为<分分钟用上C#中的委托和事件>的博文,旨在帮助C#初学者迈过委托和事件这道坎,能够用最快的速度掌握如何使用它们.如果觉得意犹未尽,或者仍然不知如何在实际应用 ...
- [ACM_模拟] ACM - Draw Something Cheat [n个长12的大写字母串,找出交集,按字母序输出]
Description Have you played Draw Something? It's currently one of the hottest social drawing games o ...
- SQL关于日期的查询
SQL查询某天的记录: datediff(day,[Datetime],'2012-08-03')=0 把Datetime换为你的相应字段: SQL查询今天的记录: datediff(day,[Dat ...
- Spring - 初始化spring容器
2016.01.12 学习linux内核的过程中发现变相的提升了自己的工程能力.以前觉得spring这些东西很复杂麻烦.然而,学了linux内核再看这些东西,发现好简单. 学习spring首先就要学习 ...