首页 > Java > java教程 > Java 中的非访问修饰符

Java 中的非访问修饰符

王林
发布: 2024-08-30 15:59:18
原创
645 人浏览过

非访问修饰符是 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中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板