java中访问修饰符_Java中的非访问修饰符是什么?

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个非访问修饰符 。 这些非访问修饰符的名称如下所示:

  1. native

    本机

  2. synchronized

    已同步

  3. transient

    短暂的

  4. volatile

    易挥发的

  5. final

    最后

  6. abstract

    抽象

  7. 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中访问修饰符

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/378687.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

mui实现分享功能_MUI 分享功能(微信、QQ 、朋友圈)

配置文件&#xff1a;manifest.jsonplus ->plugins 下边"share": {/*配置应用使用分享功能&#xff0c;参考http://ask.dcloud.net.cn/article/27*/"qq": {"appid": "",/*腾讯QQ开放平台申请应用的AppID值*/"description"…

Java 注解学习笔记

转自&#xff1a;http://wanqiufeng.blog.51cto.com/409430/458883 一、什么是java注解 注解&#xff0c;顾名思义&#xff0c;注解,就是对某一事物进行添加注释说明&#xff0c;会存放一些信息&#xff0c;这些信息可能对以后某个时段来说是很有用处的。 Java注解又叫java标注…

Prime Palindromes

博客园速度非常不稳定&#xff0c;可能要考虑换地方了。虽然我非常喜欢博客园的模板和气氛。 这个题早就知道是怎么做的了。先求出回文数在再判断是不是素数。关键是不知道区间&#xff0c;那就把所有的全部求出来。虽然可能会超时&#xff0c;但是如果使用点技巧的话还是没问题…

Opencv——DFT变换(实现两个Mat的卷积以及显示Mat的频域图像)

DFT原理&#xff1a;&#xff08;单变量离散傅里叶变换&#xff09; 数学基础&#xff1a; 任何一个函数都可以转换成无数个正弦和余弦函数的和的形式。 通常观察傅里叶变换后的频域函数可以获得两个重要的信息&#xff1a;幅频曲线和相频曲线。 在数字图像处理中的作用&#…

python方法items_Python字典items()方法与示例

python方法items字典items()方法 (Dictionary items() Method) items() method is used to get the all items as a view object, the view object represents the key-value pair of the dictionary. items()方法用于获取所有项目作为视图对象&#xff0c;该视图对象表示字典的…

基于(Python下的OpenCV)图像处理的喷墨墨滴形状规范检测

通过图像处理&#xff0c;分析数码印花的喷头所喷出来的墨滴形状&#xff0c;与标准墨滴形状对比分析&#xff0c;来判断墨水及其喷头设备的状态&#xff0c;由两部分构成 PS&#xff1a;获取墨滴形状照片和标准墨滴形状照片都是手绘的&#xff0c;将就的看吧&#xff0c;主要…

const_iterator,const 迭代器

const 迭代器:是迭代器产量&#xff0c;该迭代器的值不能被修改&#xff0c;且需要初始化&#xff0c;初始化之后不能指向其他元素。const_iterator:当我们对const_iterator类型解引用时&#xff0c;返回一个const值&#xff0c;所以只能读&#xff0c;不能写。它是一种迭代器…

临时禁止令:诺西购摩托罗拉面临流产窘境?

近日&#xff0c;美国伊利诺伊州北区法院就中国华为起诉摩托罗拉公司和诺西一案作出初步裁决&#xff0c;禁止摩托罗拉解决方案公司(Motorola Solutions)向诺西披露华为的机密资料。此判决一出&#xff0c;各方评论纷沓而来。笔者认为&#xff0c;从诺西12以美元并购摩托罗拉部…

mysql replace into 语法_mysql Replace into与Insert update

Replace intoreplace into 跟 insert 功能类似&#xff0c;不同点在于&#xff1a;replace into 首先尝试插入数据到表中&#xff0c;1. 如果发现表中已经有此行数据(根据主键或者唯一索引判断)则先删除此行数据&#xff0c;然后插入新的数据。2. 否则&#xff0c;直接插入新数…

微机原理——指令系统——传送类指令(MOV、LEA、LDS、LES、LAHF、SAHF、XCHG、XLAT、PUSH、POP、PUSHF、POPF)

博主联系方式&#xff1a; QQ:1540984562 QQ交流群&#xff1a;892023501 群里会有往届的smarters和电赛选手&#xff0c;群里也会不时分享一些有用的资料&#xff0c;有问题可以在群里多问问。 【没事儿可以到我主页看看】https://blog.csdn.net/qq_42604176 传送类指令1&…

lastindexof方法_Java Vector lastIndexOf()方法与示例

lastindexof方法向量类别的lastIndexOf()方法 (Vector Class lastIndexOf() method) Syntax: 句法&#xff1a; public int lastIndexOf (Object ob);public int lastIndexOf (Object ob, int indices);lastIndexOf() method is available in java.util package. lastIndexOf(…

李开复:微博的价值在哪里

导读&#xff1a;微博可以改变社会现象&#xff0c;可以传播信息&#xff0c;可以帮助你成长&#xff0c;可以发出你的声音。它让我们能够人人成为记者&#xff0c;让每一个转发的人都变成了一个编辑 很多人问微博是抢了谁的生意&#xff0c;开心网还是人人网&#xff1f;其实它…

mysql 任务计划 /etc/cron.d_Linux /etc/cron.d增加定时任务

一般情况下我们添加计划任务时&#xff0c;都是直接修改/etc/crontab。但是&#xff0c;不建议这样做&#xff0c;/etc/cron.d目录就是为了分项目设置计划任务而创建的。例如&#xff0c;增加一项定时的备份任务&#xff0c;我们可以这样处理&#xff1a;在/etc/cron.d目录下新…

19-Harris角点检测

角点检测顾名思义&#xff0c;就是对类似顶点的检测&#xff0c;与边缘有所区别 边缘可能在某一方向上变化不是特别明显&#xff0c;但角点在任何方向上变换都很明显 cv2.cornerHarris(img,blockSize,ksize,k) cv2.cornerHarris(gray,2,3,0.04) 参数一&#xff1a;img&#xff…

微机原理——指令系统——算数运算指令(ADD、ADC、SUB、SBB、INC、DEC、NEG、CMP、MUL、IMUL、DIV、IDIV、CBW、CWD、BCD调整)

博主联系方式&#xff1a; QQ:1540984562 QQ交流群&#xff1a;892023501 群里会有往届的smarters和电赛选手&#xff0c;群里也会不时分享一些有用的资料&#xff0c;有问题可以在群里多问问。 算数运算指令1、加减法指令ADD、ADC 、SUB 、SBB 和增量减量指令INC、DEC、NEGADD…

linux系统出现Too many open files 错误、linux too many open files

故障一、linux too many open files linux系统出现Too many open files 错误&#xff0c;这是因为文件描述符大小不够&#xff0c;或者有不正常的网络连接(Socket也是一种特殊的文件)、文件IO没有关闭并释放出文件描述符&#xff08;文件句柄&#xff0c;File Operator&#xf…

精通init ramfs构建

一、init ramfs是什么   在2.6版本的linux内核中&#xff0c;都包含一个压缩过的cpio格式的打包文件。当内核启动时&#xff0c;会 从这个打包文件中导出文件到内核的rootfs文件系统&#xff0c;然后内核检查rootfs中是否包含有init文件&#xff0c;如果有则执行它&#xff0…

python 示例_带有示例的Python date isocalendar()方法

python 示例Python date.isocalendar()方法 (Python date.isocalendar() Method) date.isocalendar() method is used to manipulate objects of date class of module datetime. date.isocalendar()方法用于操作模块datetime的日期类的对象。 It uses a date class object a…

mysql 函数重载_[赋值]函数,变量,重载 ,_第1页_169IT

[java/j2ee] java实现简单的给sql语句赋值的示例代码本身很简单。拼接sql的时候&#xff1f;不好数&#xff0c;简单的用来赋值。代码如下:/** * TODO 循环赋值,缺少的类型可随时添加 * author Lucius * param pt * param list * throws SQLException */ public static…

20-SIFT算法

import cv2 import numpy as np from matplotlib import pyplot as pltdef show_photo(name,picture):#图像显示函数cv2.imshow(name,picture)cv2.waitKey(0)cv2.destroyAllWindows()img cv2.imread(E:\Jupyter_workspace\study\data/cfx.png) gray cv2.cvtColor(img,cv2.COL…