Java修饰符/关键字
修饰符分类:
- 权限修饰符:public、protected、default、private
- 其他修饰符:abstract、static、final、transient、volatile、native、synchronized、strictfp
public:
- public的使用对象:public可以修饰 类、抽象类、接口,还可以修饰 方法和变量
- public修饰的对象可以被所以其他类访问
protected:
- protected的使用对象:protected可以修饰 方法和变量,不能修饰类(外部类) ,内部类较特殊,后面单独研究。
- protected修饰的对象可以被 同一包内的类或它的子类所访问
default:
- default的使用对象:default可以修饰 类、抽象类、接口,还可以修饰 方法和变量
- default修饰的对象可以被 同一包下的类访问
private:
- private的使用对象:private可以修饰 方法和变量,不能修饰类(外部类)
- private修饰的对象只在该类内部可见,外部不可访问,包括他的子类也不能访问、不能覆盖
换个角度看:
- 类、抽象类、接口可以被public、default修饰,default修饰的类、抽象类、接口只本包内可见
- 方法和变量可以被public、protected、default、private所有权限修饰符修饰
Q&A:
- Q:为什么类不能被private、protected修饰?
- A:private好理解,private的类完全孤立不能为外界所访问,毫无用处;就类而言如果被protected修饰时等价于default,其他的包的类根本无法引用它而产生子类
abstract:
- abstract用于修饰 类、接口或方法
- abstract修饰的类即抽象类不能初始化,含未实现即abstract的方法
- 所有的接口默认是abstract的,当然也可以显示用abstract修饰
final:
- final用于修饰 类、变量或方法
- final类不能被继承、final变量不能被修改、final方法不能被覆盖
- final变量必须初始化,可以在声明时或构造函数里面赋值
- final变量如果是引用变量,变量的value可变,但不能给引用重新赋值(新对象)
- final变量如果同时被static修饰,那必须在声明时初始化,不能放到构造函数
- 构造函数里面调用的方法最好是final的
- Oracle Tutorial: http://docs.oracle.com/javase/tutorial/java/IandI/final.html
class FinalTest{
private final List foo = new ArrayList();//
public FinalTest()
{
//foo = new ArrayList(); 如果声明时没有初始化,可以在这里初始化
}
public void updateFoo(){
foo.add( new String() );//可以改变被应用对象的值
//foo = new ArrayList(); //编译错误,不能赋予新的对象
}
}
static:
- static可修饰 变量、方法、代码块、内部类,不能修饰外部类
- static的变量或方法属于具体的类,而不是某个实例,所有实例共享该变量或方法,实例可以改变static变量,也可以通过类名直接调用
- static的方法不能直接访问 实例变量或方法,static方法里面也不能使用this
transient:
- 理解transient之前需要先理解序列号serialization
- What is serialization?
- 序列化是将对象状态持久化的一个过程,这里的持久化即将一个对象被转化成stream of bytes并存到文件中。反之,我可以从bytes里面deserialization一个对象。要实现这个功能类或接口必须继承Serialization接口
- Serialization is the process of making the object's state persistent. That means the state of the object is converted into a stream of bytes and stored in a file. In the same way, we can use the deserialization to bring back the object's state from bytes. This is one of the important concepts in Java programming because serialization is mostly used in networking programming. The objects that need to be transmitted through the network have to be converted into bytes. For that purpose, every class or interface must implement the
Serializationinterface. It is a marker interface without any methods.
- Serialization is the process of making the object's state persistent. That means the state of the object is converted into a stream of bytes and stored in a file. In the same way, we can use the deserialization to bring back the object's state from bytes. This is one of the important concepts in Java programming because serialization is mostly used in networking programming. The objects that need to be transmitted through the network have to be converted into bytes. For that purpose, every class or interface must implement the
- 序列化是将对象状态持久化的一个过程,这里的持久化即将一个对象被转化成stream of bytes并存到文件中。反之,我可以从bytes里面deserialization一个对象。要实现这个功能类或接口必须继承Serialization接口
- What is the transient keyword and it’s purpose?
- 默认情况下,序列化会保存对象的所有变量,如果你不想对象被持久化,你可以把它声明为transient。
- By default, all of object's variables get converted into a persistent state. In some cases, you may want to avoid persisting some variables because you don't have the need to persist those variables. So you can declare those variables as
transient. If the variable is declared astransient, then it will not be persisted. That is the main purpose of thetransientkeyword.
- By default, all of object's variables get converted into a persistent state. In some cases, you may want to avoid persisting some variables because you don't have the need to persist those variables. So you can declare those variables as
- 默认情况下,序列化会保存对象的所有变量,如果你不想对象被持久化,你可以把它声明为transient。
- Example
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable; class NameStore implements Serializable {
private String firstName;
private transient String middleName;
private String lastName; public NameStore (String fName, String mName, String lName) {
this.firstName = fName;
this.middleName = mName;
this.lastName = lName;
} public void func(){
System.out.println("I'm func");
} public String toString() {
StringBuffer sb = new StringBuffer(40);
sb.append("First Name : ");
sb.append(this.firstName);
sb.append("Middle Name : ");
sb.append(this.middleName);
sb.append("Last Name : ");
sb.append(this.lastName);
return sb.toString();
}
} public class TransientExample {
public static void main(String args[]) throws Exception {
NameStore nameStore = new NameStore("Steve", "Middle", "Jobs");
ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream(
"nameStore"));
// writing to object
o.writeObject(nameStore);
o.close(); // reading from object
ObjectInputStream in = new ObjectInputStream(new FileInputStream(
"nameStore"));
NameStore nameStore1 = (NameStore) in.readObject();
nameStore1.func();
System.out.println(nameStore1);
}
/**Output
*
*I'm func
*First Name : SteveMiddle Name : nullLast Name : Jobs
*
***/
}
native:
- 标识一个方法将会用其他语言实现而不是Java,It works together with JNI(Java Native Interface)
- Native方法曾经主要用于写一些对性能要求很高的代码块,随之Java变得越来越快,Native方法变得不常用了,Native method is currently needed when
- 调用其他语言写的library
- 你需要访问那些Java不可及的系统资源,只能Native方法调用其他语言实现
- Native的方法就像Abstract的方法一样没有方法体。
strictfp:
- Strictfp ensures that you get exactly the same results from your floating point calculations on every platform. If you don't use strictfp, the JVM implementation is free to use extra precision where available.
- From the JLS:
- Within an FP-strict expression, all intermediate values must be elements of the float value set or the double value set, implying that the results of all FP-strict expressions must be those predicted by IEEE 754 arithmetic on operands represented using single and double formats. Within an expression that is not FP-strict, some leeway is granted for an implementation to use an extended exponent range to represent intermediate results; the net effect, roughly speaking, is that a calculation might produce "the correct answer" in situations where exclusive use of the float value set or double value set might result in overflow or underflow.
- In other words, it's about making sure that Write-Once-Run-Anywhere actually means Write-Once-Get-Equally-Wrong-Results-Everywhere.
- With strictfp your results are portable, without it they are more likely to be accurate.
volatile:
http://stackoverflow.com/questions/106591/do-you-ever-use-the-volatile-keyword-in-java
synchronized:
Java修饰符/关键字的更多相关文章
- Java修饰符关键字的顺序
Java语言规范建议按以下顺序列出修饰符: 1. Annotations 2. public 3. protected 4. private 5. abstract 6. static 7. fina ...
- java的基础语法(标识符 修饰符 关键字)
Java 基础语法 一个 Java 程序可以认为是一系列对象的集合,而这些对象通过调用彼此的方法来协同工作.下面简要介绍下类.对象.方法和实例变量的概念. 对象:对象是类的一个实例,有状态和行为.例如 ...
- JAVA修饰符类型(public,protected,private,friendly)
转自:http://www.cnblogs.com/webapplee/p/3771708.html JAVA修饰符类型(public,protected,private,friendly) publ ...
- Java修饰符关键词大全
所以我以此主题写了这篇文章.这也是一个可用于测试你的计算机科学知识的面试问题. Java修饰符是你添加到变量.类和方法以改变其含义的关键词.它们可分为两组: 访问控制修饰符 非访问修饰符 让我们先来看 ...
- JAVA修饰符类型(转帖)
JAVA修饰符类型(public,protected,private,friendly) public的类.类属变量及方法,包内及包外的任何类均可以访问:protected的类.类属变量及方法,包内的 ...
- java修饰符public final static abstract transient
JAVA 修饰符public final static abstract transient 关键字: public final static abstract ... 1.public prot ...
- Java基础之Java 修饰符
前言:Java内功心法之Java 修饰符,看完这篇你向Java大神的路上又迈出了一步(有什么问题或者需要资料可以联系我的扣扣:734999078) Java语言提供了很多修饰符,主要分为以下两类: 访 ...
- 浅析java修饰符之public default protected private static final abstract
浅析java修饰符之public default protected private static final abstract 一 修饰符的作用:用来定义类.方法或者变量,通常放在语句的最前端 ...
- 【java初探外篇01】——关于Java修饰符
本文记录在学习Java语言过程中,对碰到的修饰符的一些疑问,在这里具体的拿出来详细学习和记录一下,以作后续参考和学习. Java修饰符 Java语言提供了很多修饰符,但主要分两类: 访问修饰符 非访问 ...
随机推荐
- STL string大小写 转换
std::string data = "This is a sample string."; // convert string to upper case std::for_ea ...
- MVC4.0 里的分析器错误
这种错误有很多,今天碰到了,代码段写在if里就回出错,应该是认冲了吧 @if (Web.Common.UserInfo.CurrentUserInfo != null) ...
- maven spring3.2.5
出现的情形: 开发环境: spring3.2.5 + springmvc +spirngDATA +maven 一. 偶然的spring Junit4测试 加载applicationContext.x ...
- overflow: auto;溢出自动显示
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Fast Walsh–Hadamard transform
考虑变换 $$\hat{A_x} = \sum_{i\ or\ x = x}{ A_i }$$ 记 $S_{t}(A,x) = \sum_{c(i,t)\ or\ c(x,t)=c(x,t),\ i ...
- linux 安装输入法
简述 Ubuntu16.04安装完后,和12.04以及14.04都不一样,并没有中文输入功能.于是搜索一些安装中文输入法的方法. 开始安装了ibus pinyin输入法,但是系统重启之后发现有些时候不 ...
- HTML5新增的结构元素
HTML5的结构 一:新增的主体结构元素 在HTML5中,为了使文档的结构更加清晰明确,追加了几个与页眉,页脚内容区块等文档结构相关联的结构元素. 1.1article元素 article元素代表文档 ...
- R: data.frame 数据框的:查询位置、排序(sort、order)、筛选满足条件的子集。。
################################################### 问题:数据框 data.frame 查.排序等, 18.4.27 怎么对数据框 data.f ...
- 【转】JAVA输出内容打印到TXT以及不同系统中如何换行
JAVA输出内容打印到TXT以及不同系统中如何换行 http://xiyang.09.blog.163.com/blog/static/59827615201172552755293/ 2011-08 ...
- ExecuteNonQuery(),ExecuteScalar(),ExecuteReader的用法-转
using System.Data.SqlClient;...SqlConnection conn = new SqlConnection(@"server=ws7\leosql;datab ...