Pengubah Bukan Akses ialah kata kunci yang diperkenalkan dalam Java 7 untuk memberitahu JVM tentang gelagat, kaedah atau pembolehubah kelas, dsb. Itu membantu memperkenalkan fungsi tambahan, seperti kata kunci akhir yang digunakan untuk menunjukkan bahawa pembolehubah tidak boleh dimulakan dua kali. Terdapat sejumlah 7 pengubah bukan akses diperkenalkan.
Di bawah ialah jenis Pengubah Suai Bukan Akses dalam Java:
IKLAN Kursus Popular dalam kategori ini JAVA MASTERY - Pengkhususan | 78 Siri Kursus | 15 Ujian Olok-olokPengubah suai ini boleh digunakan dengan:
Kod:
final class Honda{ public void myFun1(){ System.out.println("Honda Class"); } } class Bike extends Honda{ public void myFun1(){ System.out.println("Bike Class"); } }
Output:
Kod:
class Honda{ public final void myFun1(){ System.out.println("Honda Class"); } } class Bike extends Honda{ public void myFun1(){ System.out.println("Bike Class"); } }
Output:
Kod:
public abstract class MyActivity{ public MyActivity(){ } public final String myFun1(){ } }
Contoh: abstrak awam void fun1();
Kod:
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"); } }
Output:
Kata kunci ini membantu menghalang capaian satu kaedah oleh berbilang utas secara serentak, dengan itu menyegerakkan aliran program dan mengeluarkan hasil yang diingini menggunakan ciri multithreading.
Kod:
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"); } } }
Output:
Pembolehubah ini digunakan untuk pengurusan memori dan perkara pertama dirujuk semasa memuatkan kelas. Ahli-ahli ini dilayan pada peringkat kelas; oleh itu, mereka tidak boleh dipanggil menggunakan objek; sebaliknya, nama kelas digunakan untuk merujuk kepada mereka.
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:
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"); } }
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:
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:
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.
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 –
Atas ialah kandungan terperinci Pengubah Suai Bukan Akses di Java. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!