浅谈Java编程中的synthetic关键字

 更新时间:2017年09月14日 16:59:30   投稿:mengwei  
这篇文章主要介绍了浅谈Java编程中的synthetic关键字的相关内容,包括其简单的介绍和实例,需要的朋友可以了解下。

java synthetic关键字。有synthetic标记的field和method是class内部使用的,正常的源代码里不会出现synthetic field。小颖编译工具用的就是jad.所有反编译工具都不能保证完全正确地反编译class。所以你不能要求太多。

下面我给大家介绍一下synthetic

下面的例子是最常见的synthetic field

Java代码

class parent {
		public void foo() {
		}
		class inner {
			inner() {
				foo();
			}
		}
	}

非static的inner class里面都会有一个this$0的字段保存它的父对象。编译后的inner class 就像下面这样:
Java代码

class parent$inner{
	synthetic parent this$0;
	parent$inner(parent this$0)
	{
		this.this$0 = this$0;
		this$0.foo();
	}
}

所有父对象的非私有成员都通过 this$0来访问。
还有许多用到synthetic的地方。比如使用了assert 关键字的class会有一个synthetic static boolean $assertionsDisabled 字段
使用了assert的地方

assert condition;
在class里被编译成
Java代码

if(!$assertionsDisabled && !condition){
	throw new AssertionError();
	}

还有,在jvm里,所有class的私有成员都不允许在其他类里访问,包括它的inner class。在java语言里inner class是可以访问父类的私有成员的。在class里是用如下的方法实现的:

Java代码

class parent{
		private int value = 0;
		synthetic static int access$000(parent obj)
		{
			return value;
		}
	}

在inner class里通过access$000来访问value字段。
synthetic的概念

According to the JVM Spec: "A class member that does not appear in the source code must be marked using a Synthetic attribute." Also, "The Synthetic attribute was introduced in JDK release 1.1 to support nested classes and interfaces."

I know that nested classes are sometimes implemented using synthetic fields and synthetic contructors, e.g. an inner class may use a synthetic field to save a reference to its outer class instance, and it may generate a synthetic contructor to set that field correctly. I'm not sure if it Java still uses synthetic constructors or methods for this, but I'm pretty sure I did see them used in the past. I don't know why they might need synthetic classes here. On the other hand, something like RMI or java.lang.reflect.Proxy should probably create synthetic classes, since those classes don't actually appear in source code. I just ran a test where Proxy did not create a synthetic instance, but I believe that's probably a bug.

Hmm, we discussed this some time ago back here. It seems like Sun is just ignoring this synthetic attribute, for classes at least, and we should too.

注意上文的第一处黑体部分,一个类的复合属性表示他支持嵌套的类或者接口。

注意上文的第二处黑体部分,说明符合这个概念就是OO思想中的类的复合,也就是只要含有其它类的引用即为复合。

总结

以上就是本文关于Java编程中的synthetic关键字的全部内容,希望对大家能有所帮助。感谢大家对本站的支持。

相关文章

  • Java实现无头双向链表操作

    Java实现无头双向链表操作

    这篇文章主要为大家详细介绍了Java实现无头双向链表的基本操作,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-01-01
  • 8个Spring事务失效场景详解

    8个Spring事务失效场景详解

    相信大家对Spring种事务的使用并不陌生,但是你可能只是停留在基础的使用层面上。今天,我们就简单来说下Spring事务的原理,然后总结一下spring事务失败的场景,并提出对应的解决方案,需要的可以参考一下
    2022-12-12
  • Springboot使用异步请求提高系统的吞吐量详解

    Springboot使用异步请求提高系统的吞吐量详解

    这篇文章主要介绍了Springboot使用异步请求提高系统的吞吐量详解,和同步请求相对,异步不需要等待响应,随时可以发送下一次请求,如果是同步请求,需要将信息填写完整,再发送请求,服务器响应填写是否正确,再做修改,需要的朋友可以参考下
    2023-08-08
  • JavaSE实现图书管理系统的示例代码

    JavaSE实现图书管理系统的示例代码

    这篇博客是在学习了一部分Java基础语法之后的练习项目,通过这个小项目的练习,对Java中的类和对象,抽象类和接口等进行熟悉理解。快跟随小编一起学习学习吧
    2022-08-08
  • Java实现去重的方法详解

    Java实现去重的方法详解

    austin支持两种去重的类型:N分钟相同内容达到N次去重和一天内N次相同渠道频次去重,这篇文章就来和大家讲讲这两种去重的具体实现,需要的可以参考一下
    2023-06-06
  • Java编程简单应用

    Java编程简单应用

    本文主要介绍了三个简单Java小程序———1、HelloWorld(HelloWorld的来源);2、输出个人信息3、输出特殊图案。具有很好的参考价值,下面跟着小编一起来看下吧
    2017-02-02
  • Java中 SLF4J和Logback和Log4j和Logging的区别与联系

    Java中 SLF4J和Logback和Log4j和Logging的区别与联系

    这篇文章主要介绍了Java中 SLF4J和Logback和Log4j和Logging的区别与联系,文章通过围绕主题展开详细的内容介绍,具有一定的参考几种,感兴趣的小伙伴可以参考一下
    2022-09-09
  • Idea中mapper注入报错问题及解决

    Idea中mapper注入报错问题及解决

    这篇文章主要介绍了Idea中mapper注入报错问题及解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-03-03
  • 解决SpringBoot连接SqlServer出现的问题

    解决SpringBoot连接SqlServer出现的问题

    在尝试通过SSL与SQL Server建立安全连接时,如果遇到“PKIX path building failed”错误,可能是因为未能正确配置或信任服务器证书,当"Encrypt"属性设置为"true"且"trustServerCertificate"属性设置为"false"时,要求驱动程序使用安全套接字层(SSL)加密与SQL Server建立连接
    2024-10-10
  • spring boot 部署为jar包的方法示例

    spring boot 部署为jar包的方法示例

    本篇文章主要介绍了spring boot 部署为jar的方法示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-10-10

最新评论