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
.minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } } .minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } }

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
.minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } } .minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } }

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

java中访问修饰符_Java中的非访问修饰符是什么?相关推荐

  1. java中访问修饰符_Java中的访问修饰符介绍

    java中访问修饰符 什么是访问修饰符? (What are Access Modifiers?) Have you ever wanted to define how people would ac ...

  2. java的四个访问修饰符_Java中的四种访问修饰符

    Java中修饰符分为两种:访问修饰符和非访问修饰符.修饰符中,有一些修饰符可以既可以修饰类,也可以修饰方法,但是有一些修饰符只能修饰符方法. 今天这篇文章先介绍一下四种访问修饰符. 1.private ...

  3. java 权限修饰符_java中4种访问权限修饰符

    在Java编程语言中有四种权限访问控制符,这四种访问权限的控制符能够控制类中成员的可见性. 一.public (1) 定义:public是公共的,被public所修饰的成员可以在任何类中都能被访问到. ...

  4. java中有哪些访问修饰符_java中四种访问修饰符

    Java中的四种访问修饰符:public.protected.default(无修饰符,默认).private. 四种修饰符可修饰的成分(类.方法.成员变量) public protected def ...

  5. java四种修饰符_java中的四种修饰符

    在编程过程中,经常会遇到四种修饰符来控制访问权限.之前对这个知识点没有研究过,一直是一知半解,每次遇到问题都模棱两可,不能给出一个确切的答案.近几天系统的看了看,也有了自己的一点心得体会. 正文: 先 ...

  6. java中类的修饰符_Java中的类修饰符

    之前每次写小测试程序的时候,总是把一个类放在一个Java文件中,按理说这样比较规范,可主要原因是我是在不知道怎么在一个文件里放多个类,可见java基础有多差了...只要把类的属性改成默认的就可以了,也 ...

  7. java对于数组的定义_Java中方法的定义与使用,以及对数组的初步了解。

    方法 方法的含义 定义: 方法就是用来完成解决某件事情或实现某个功能的办法. 方法实现的过程中,会包含很多条语句用于完成某些有意义的功能--通常是处理文本,控制输入或计算数值. 我们可以通过在程序代码 ...

  8. Java NIO_I/O基本概念_Java中的缓冲区(Buffer)_通道(Channel)_网络I/O

    I/O基本概念 缓冲区基础 缓冲区是I/O的基础, 进程使用read(), write()将数据读出/写入从缓冲区中; 当缓冲区写满, 内核向磁盘发出指令, 将缓冲区中数据写入磁盘中(这一步不需要CP ...

  9. java的静态如何理解_java中的静态是什么?如何理解?

    在Java语言中,static表示"静态"的意思,使用场景可以用来修饰成员变量和成员方法,当然也可以是静态代码块.static的主要作用在于创建独立于具体对象的域变量或者方法. 格 ...

最新文章

  1. Invalid argument(s) 'pool_size' sent to create_engine(), using configuration
  2. 正则表达式之IP地址检验
  3. php中怎么定义page,css @page的使用与定义详解
  4. 分享一个多线程实现[冒泡][选择][二分法]排序的例子
  5. php中empty功能,在php中empty函数起什么作用呢?
  6. Wamp修改httpd.conf中的DocumentRoot不生效解决办法
  7. Oracle查看用户、用户权限、用户表空间、用户默认表空间
  8. ASP.NET MVC 控制器激活(三)
  9. 注册事件的两种方式(传统注册事件、方法监听注册事件)
  10. 关于react上线系列问题及解决方案
  11. onvif开发踩坑【二】鉴权失败
  12. word文件在线转换成pdf
  13. java3D实现空间立方体,纯CSS3实现一个旋转的3D立方体盒子
  14. linux 向终端 发送消息,Linux向不同的连接终端窗口发送消息
  15. AURIX TC397 CAN MCMCAN
  16. TextWrangler——一款不知为何而生的编辑器
  17. TP开发的源码或素材付费下载站网站源码+整体不错
  18. 多次办理这项公积金业务都涉及到查询信用报告,是否会影响将来申请贷款?
  19. CSS3过渡:在2个不同的渐变色之间进行动画处理
  20. 分享:ThinkPad E40无线网卡驱动安装 FOR CENTOS6.3

热门文章

  1. c语言诡异程序,为什么C语言诡异离奇、缺陷重重,却获得了巨大的成功?
  2. mac solr mysql 配置文件_Solr配置文件浅析
  3. Jquery中如何获取元素的文本,值,属性和内容
  4. 分享几道经典的javascript面试题
  5. 备忘录——通过RVA计算文件位置
  6. MYSQL AND OR的联用
  7. xml配置文件推荐方式
  8. linux之x86裁剪移植---ffmpeg的H264解码显示(420、422)
  9. (转)会议期刊论文发表介绍(计算机科学领域)
  10. [Algorithm] 字符串匹配算法——KMP算法