java中訪問修飾符
Java非訪問修飾符 (Java non access modifiers)
We have 7 non-access modifiers in Java. The name of these non-access modifiers are given below,
Java中有7個非訪問修飾符 。 這些非訪問修飾符的名稱如下所示:
native
本機
synchronized
已同步
transient
短暫的
volatile
易揮發的
final
最后
abstract
抽象
static
靜態的
We will learn all the non access modifiers one by one...
我們將一一學習所有非訪問修飾符...
1)本地的 (1) native)
"native" is a keyword which is introduced in java.
“ native”是在Java中引入的關鍵字。
"native" is the modifier applicable for methods only but it is not applicable for variable and classes.
“本機”是僅適用于方法的修飾符,但不適用于變量和類。
The native methods are implemented in some other language like C, C++, etc.
本機方法以其他一些語言(如C,C ++等)實現。
The purpose of the native method is to improve the performance of the system.
本機方法的目的是提高系統性能。
We know that the implementation of native methods is available in other languages so we don't need to care about the implementation.
我們知道本機方法的實現在其他語言中也可用,因此我們不需要關心實現。
Example: We will see the way of writing native methods
示例:我們將看到編寫本機方法的方式
class Native {
static {
// Load Native Library
System.loadLibrary("native library");
}
// Native Method Declaration
public native void display();
}
class Main {
public static void main(String[] args) {
Native native = new Native();
native.display();
}
}
2)同步 (2) synchronized)
"synchronized" is the keyword applicable for methods and block.
“已同步”是適用于方法和塊的關鍵字。
"synchronized" keyword is not applicable for classes and variables.
“ synchronized”關鍵字不適用于類和變量。
"synchronized" keyword is useful for multithreading if we declare a method as synchronized then at a time only one thread is allowed to operate on an object.
如果我們將一個方法聲明為“ synchronized”,那么“ synchronized”關鍵字對于多線程很有用,那么一次只能在一個對象上運行一個線程。
Example:
例:
class SynchronizedDisplay {
public synchronized void display(String msg) {
for (int i = 0; i < 2; ++i) {
System.out.println(msg);
try {
Thread.sleep(500);
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}
}
class MyThread extends Thread {
SynchronizedDisplay sd;
MyThread(SynchronizedDisplay sd) {
this.sd = sd;
}
public void run() {
sd.display("hi");
}
}
class SynchronizedClass {
public static void main(String[] args) {
SynchronizedDisplay sd1 = new SynchronizedDisplay();
MyThread mt1 = new MyThread(sd1);
mt1.start();
MyThread mt2 = new MyThread(sd1);
mt2.start();
}
}
Output
輸出量
E:\Programs>javac SynchronizedClass.java
E:\Programs>java SynchronizedClass
hi
hi
hi
hi
3)瞬態 (3) transient)
"transient" is a keyword introduced in java.
“ transient”是Java中引入的關鍵字。
"transient" is the modifier applicable only for variables.
“瞬態”是僅適用于變量的修飾符。
"transient" is the modifier not applicable for classes and methods.
“瞬態”是不適用于類和方法的修飾符。
"transient" is useful for serialization because at the time of serialization if we don't want to save the value of the variable to meet some security constraints.
“瞬態”對于序列化很有用,因為在序列化時,如果我們不想保存變量的值以滿足某些安全性約束。
Example:
例:
Let suppose we have a class named Transient in that class we have two-three data member fname (first name), lname (last name) and address, so the address member declared as transient so its values will not be serialized (i.e. in case of deserialization of an object we will get the default value of address variable and its defined value will be erased).
假設我們有一個名為Transient的類,在該類中,我們有2-3個數據成員fname (名字), lname (姓氏)和address ,因此地址成員聲明為transient,因此其值將不被序列化(例如,對對象進行反序列化后,我們將獲取地址變量的默認值,并將其定義的值刪除)。
import java.io.*;
class Serialization implements Serializable {
public String fname, lname;
transient String address;
public Serialization(String fname, String lname, String address) {
this.fname = fname;
this.lname = lname;
this.address = address;
}
}
public class Deserialization {
public static void main(String[] args) {
Serialization serialize = new Serialization("Ronit", "Jain", "Mayur vihar 1");
try {
FileOutputStream fos = new FileOutputStream("E:\\Programs\\myjava.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(serialize);
oos.close();
fos.close();
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
serialize = null;
try {
FileInputStream fis = new FileInputStream("E:\\Programs\\myjava.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
serialize = (Serialization) ois.readObject();
ois.close();
fis.close();
System.out.println("His full name and address is :" + serialize.fname + " " + serialize.lname + " " + serialize.address);
} catch (IOException ex) {
System.out.println(ex.getMessage());
} catch (ClassNotFoundException ex) {
System.out.println(ex.getMessage());
}
}
}
Output
輸出量
E:\Programs>javac Deserialization.java
E:\Programs>java Deserialization
His full name and address is :Ronit Jain null
4)易揮發 (4) volatile)
"volatile" is a keyword which is introduced in java.
“ volatile”是在Java中引入的關鍵字。
"volatile" is the modifier applicable only for variables but not for methods and classes.
“ volatile”是僅適用于變量的修飾符,不適用于方法和類。
If the value of variable keep on changing such type of variable we have to declare with the volatile modifier.
如果變量的值繼續更改此類變量,則必須使用volatile修飾符進行聲明。
Any intermediate operation will be performed in local copy instead of the final copy.
任何中間操作都將在本地副本而不是最終副本中執行。
Example:
例:
class VolatileVariable {
// volatile keyword here makes sure that
// the changes made in one class are
// immediately reflect in other class
static volatile int volatile_var = 10;
}
class Main {
public static void main(String[] args) {
System.out.println("The previous value of volatile variable in one class is " + VolatileVariable.volatile_var);
VolatileVariable.volatile_var++;
System.out.println("The value changes made to the volatile variable in other class is" + VolatileVariable.volatile_var);
}
}
Output
輸出量
E:\Programs>javac Main.java
E:\Programs>java Main
The previous value of volatile variable in one class is 10
The value changes made to the volatile variable in other class is 11
5)最終 (5) final)
"final" is a keyword which is introduced in java.
“ final”是在Java中引入的關鍵字。
"final" is the modifier applicable for methods, classes, and variables.
“最終”是適用于方法,類和變量的修飾符。
We cannot override in child class.
我們不能在子類中重寫。
Example: Declare class as "final" and variable as final and method as final
示例:將類聲明為“ final”,將變量聲明為final,將方法聲明為final
final class Final {
final String str = "we are accessible final variable";
final void printMethod() {
System.out.println("we are in final method");
}
public static void main(String[] args) {
Final f = new Final();
System.out.println("final variable :" + f.str);
f.printMethod();
}
}
Output
輸出量
E:\Programs>javac Final.java
E:\Programs>java Final
final variable :we are accessible final variable
we are in final method.
6)摘要 (6) abstract)
"abstract" is a keyword which is introduced in java.
“抽象”是在Java中引入的關鍵字。
"abstract" is the modifier applicable for classes and methods.
“抽象”是適用于類和方法的修飾語。
If a class is abstract then we need to implement all methods of abstract class in our class.
如果一個類是抽象的,那么我們需要在我們的類中實現所有抽象類的方法。
Example:
例:
abstract class AbstractClass {
abstract void printMethod();
}
public class AbstractImplementation {
public static void main(String[] args) {
AbstractClass ac = new AbstractClass() {
void printMethod() {
System.out.println("Hi, We are in abstract class");
}
};
ac.printMethod();
}
}
Output
輸出量
E:\Programs>javac AbstractImplementation.java
E:\Programs>java AbstractImplementation
Hi, We are in abstract class
7)靜態 (7) static)
"static" is a keyword introduced in java.
“ static”是Java中引入的關鍵字。
"static" member create one copy of the whole program and share it with other objects of the same program.
“靜態”成員創建整個程序的一個副本,并與同一程序的其他對象共享。
"static" can access only static methods.
“靜態”只能訪問靜態方法。
Example:
例:
class StaticClass {
public static int div(int a, int b) {
return a / b;
}
}
class Main {
public static void main(String[] args) {
int p = 20, q = 10;
int div = StaticClass.div(p, q);
System.out.println("The div of p , q is" + div);
}
}
Output
輸出量
E:\Programs>javac Main.java
E:\Programs>java Main
The div of p , q is2
翻譯自: https://www.includehelp.com/java/what-are-the-non-access-modifiers-in-java.aspx
java中訪問修飾符