首頁  >  文章  >  Java  >  Java 中的非存取修飾符

Java 中的非存取修飾符

王林
王林原創
2024-08-30 15:59:18569瀏覽

非存取修飾符是 Java 7 中引入的關鍵字,用於通知 JVM 有關類別的行為、方法或變數等的資訊。這有助於引入其他功能,例如用於指示變數不能被初始化兩次的 Final 關鍵字。總共引入了 7 個非訪問修飾符。

  1. 靜態
  2. 決賽
  3. 摘要
  4. 已同步
  5. 短暫
  6. 嚴格fp
  7. 本地人

Java 中的非存取修飾符類型

以下是 Java 中非存取修飾符的類型:

廣告 該類別中的熱門課程 JAVA 掌握 - 專業化 | 78 課程系列 | 15 次模擬測驗

1.最終非訪問修飾符

此修飾符可應用於:

  1. 班級
  2. 方法
  3. 實例變數
  4. 局部變數
  5. 方法參數

Java 中的非存取修飾符

  • Final Class:當我們想要限制任何其他類別對某個類別的繼承時,可以使用 Final 關鍵字。例如,如果我們有本田的最終類,那麼任何擴展此類的嘗試都可能導致編譯時錯誤。

代碼:

final class Honda{
public void myFun1(){
System.out.println("Honda Class");
}
}
class Bike extends Honda{
public void myFun1(){
System.out.println("Bike Class");
}
}

輸出:

Java 中的非存取修飾符

  • Final 方法:Final 關鍵字用於指示 Java 執行時間環境該方法不應在其任何子類別中被重寫。

代碼:

class Honda{
public final void myFun1(){
System.out.println("Honda Class");
}
}
class Bike extends Honda{
public void myFun1(){
System.out.println("Bike Class");
}
}

輸出:

Java 中的非存取修飾符

  • Final Variable:final 關鍵字與變數一起使用,以限制變數值的任何修改,從而指示 JVM 將其視為常數。這意味著最終變數只能初始化一次。

2.抽象非存取修飾符

Java 中的非存取修飾符

  • 抽象類別:一個類別被宣告為抽象類,表示該類別不能被實例化,也就是說該類別不能形成任何對象,但可以被繼承。儘管如此,該類別仍然有一個建構函數,該構造函數將在其子類別的建構函數內部呼叫。它可以包含抽象方法和最終方法,其中抽象方法將在子類別中被重寫。

代碼:

public abstract class MyActivity{
public MyActivity(){
}
public final String myFun1(){
}
}
  • 抽象方法:抽象方法是沒有任何定義的方法。它僅包含方法的簽名,旨在表明這些需要在子類別中重寫。

範例: public abstract void fun1();

代碼:

abstract class Electronics
{
abstract void display();
abstract void display(String msg);
}
class Computers extends Electronics
{
@Override
void display() {
System.out.println("Abstract method is called");
}
@Override
void display(String txt) {
System.out.println(txt);
}
}
public class AbstractDemo {
public static void main(String[] args) {
Computers obj=new Computers();
obj.display();
obj.display("Method with arguments");
}
}

輸出:

Java 中的非存取修飾符

3.同步非存取修飾符

Java 中的非存取修飾符

此關鍵字有助於防止多個執行緒同時存取一個方法,從而同步程式流程並使用多執行緒功能得出所需的結果。

代碼:

class Person1
{
public synchronized void sendFun(String txt)
{
System.out.println("Sending message\t" + txt );
try
{
Thread.sleep(1000);
}
catch (Exception e)
{
System.out.println("Thread interrupted.");
}
System.out.println("\n" + txt + "Sent");
}
}
class DemoThread extends Thread
{
private String txt;
Person1  person;
DemoThread(String m,  Person1 obj)
{
txt = m;
person = obj;
}
public void run()
{
synchronized(person)
{
person.sendFun(txt);
}
}
}
public class HelloWorld
{
public static void main(String args[])
{
Person1 snd = new Person1();
DemoThread S1 =
new DemoThread( " Hi " , snd );
DemoThread S2 =
new DemoThread( " Bye " , snd );
S1.start();
S2.start();
// wait for threads to end
try
{
S1.join();
S2.join();
}
catch(Exception e)
{
System.out.println("Interrupted");
}
}
}

輸出:

Java 中的非存取修飾符

4.靜態非存取修飾符

Java 中的非存取修飾符

此變數用於記憶體管理,也是載入類別時首先引用的變數。這些成員受到班級層級的待遇;因此,不能使用物件來呼叫它們;相反,類別的名稱用於引用它們。

  • Static Variable: If a variable is declared as static, then only a single copy of the variable is created and shared among all the objects. Thus any change made to the variable by one object will be reflected in other others. Therefore,  the variables that hold value on the class level is declared as static.
  • Static Class: Static keyword can only be used with nested classes.
  • Static Methods: Since Static Methods are referenced by class name thus can only access static member variables and other static methods. Also, these methods cannot be referred to using this or super pointer. The main method is the most common example of a static method that always get loaded while its class is being loaded.
  • Static Block: This is said to be a block being used to perform certain operations while class is being loaded. Since it is static thus can use only static members of the class.

Code:

public class Demo
{
// static variable
static int x = 10;
static int y;
//static class
public static class DemoInnerClass{
static int z=10;
}
// static block
static {
System.out.println("Static block initialized.");
y = x + 4;
}
//static method
public static void main(String[] args)
{
System.out.println("from main");
System.out.println("Value of x : "+x);
System.out.println("Value of y : "+y);
System.out.println("Value of z : "+DemoInnerClass.z);
}
}

Output:

Java 中的非存取修飾符

5. Native Non Access Modifier

Java 中的非存取修飾符

The native keyword is used only with the methods to indicate that the particular method is written in platform -dependent. These are used to improve the system’s performance, and the existing legacy code can be easily reused.

Note: Static, as well as abstract methods, cannot be declared as native.

Example: Consider a function myfun1 in class NativeDemo that is written in C++. To use this code, we will create a link library mylib1 and load it using the class’s static block.

public class DateTimeUtils {
public native String getSystemTime();
static {
System.loadLibrary("nativedatetimeutils");
}
}

6. Strictfp Non-Access Modifier

Java 中的非存取修飾符

  • Strictfp Class / Method: This keyword is used to ensure that results from an operation on floating-point numbers brings out the same results on every platform. This keyword can not be used with abstract methods, variables or constructors as these need not contain operations.

Code:

public class HelloWorld
{
public strictfp double calSum()
{
double n1 = 10e+07;
double n2 = 9e+08;
return (n1+n2);
}
public static strictfp void main(String[] args)
{
HelloWorld t = new HelloWorld ();
System.out.println("Result is -" + t.calSum());
}
}

Output:

Java 中的非存取修飾符

7. Transient Non-Access Modifier

While transferring the data from one end to another over a network, it must be serialised for successful receiving of data, which means convert to byte stream before sending and converting it back at receiving end. To tell JVM about the members who need not undergo serialization instead of being lost during transfer, a transient modifier comes into the picture.

Syntax:

private transient member1;

Code:

import java.io.*;
class Demo implements Serializable
{
int x = 10;
transient int y = 30;
transient static int z = 40;
transient final int d = 50;
public static void main(String[] args) throws Exception
{
Demo input = new Demo();
FileOutputStream tos = new FileOutputStream("abc.txt");
ObjectOutputStream tin = new ObjectOutputStream(tos);
tin.writeObject(input);
FileInputStream fis = new FileInputStream("abc.txt");  ObjectInputStream ois = new ObjectInputStream(fis);
Demo output = (Demo)ois.readObject();
System.out.println("x = " + output.x);
System.out.println("y = " + output.y);
System.out.println("z = " + output.z);
System.out.println("d = " + output.d);
}
}

Output:

Java 中的非存取修飾符

Conclusion

Non-access modifiers are the type of modifiers that tell JVM about the behavior of classes, methods, or variables defined and prepared accordingly. It also helps in synchronizing the flow as well as displaying similar results from operations being performed irrespective of the platform used for execution.

Recommended Article

This is a guide to Non Access Modifiers in Java. Here we discuss the Types of Non Access Modifiersand their methods and code implementation in Java. You can also go through our other suggested articles to learn more –

  1. Layout in Java
  2. Java Compilers
  3. Merge Sort In Java
  4. Java BufferedReader

以上是Java 中的非存取修飾符的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn