publicclassTest{publicstaticTestt1=newTest();{System.out.println("blockA");}static{System.out.println("blockB");}publicstaticvoidmain(String[]args){Testt2=newTest();}}为什么程序会先输出普通构造块的内容?"C:\ProgramFiles\Java\jdk-9.0.4\bin\java"-agentlib:jdwp=transport=dt_socket,address=127.0.0.1:59784,suspend=y,server=n-javaagent:C:\Users\Administrator\.IntelliJIdea2018.1\system\captureAgent\debugger-agent.jar=C:\Users\Administrator\AppData\Local\Temp\capture1160.props-Dfile.encoding=UTF-8-classpath"F:\chromedownloads\tale-master\demo\target\classes;E:\mavenRepository\org\springframework\cloud\spring-cloud-starter-eureka\1.4.2.RELEASE\spring-cloud-starter-eureka-1.4.2.RELEASE.jar;E:\mavenRepository\org\springframework\cloud\spring-cloud-starter-netflix-eureka-client\1.4.2.RELEASE\spring-cloud-starter-netflix-eureka-client-1.4.2.RELEASE.jar;E:\mavenRepository\org\springframework\boot\spring-boot-starter-web\1.5.10.RELEASE\spring-boot-starter-web-1.5.10.RELEASE.jar;E:\mavenRepository\org\springframework\boot\spring-boot-starter\1.5.10.RELEASE\spring-boot-starter-1.5.10.RELEASE.jar;E:\mavenRepository\org\springframework\boot\spring-boot-starter-logging\1.5.10.RELEASE\spring-boot-starter-logging-1.5.10.RELEASE.jar;E:\mavenRepository\ch\qos\logback\logback-classic\1.1.11\logback-classic-1.1.11.jar;E:\mavenRepository\ch\qos\logback\logback-core\1.1.11\logback-core-1.1.11.jar;E:\mavenRepository\org\slf4j\jcl-over-slf4j\1.7.25\jcl-over-slf4j-1.7.25.jar;E:\mavenRepository\org\slf4j\jul-to-slf4j\1.7.25\jul-to-slf4j-1.7.25.jar;E:\mavenRepository\org\slf4j\log4j-over-slf4j\1.7.25\log4j-over-slf4j-1.7.25.jar;E:\mavenRepository\org\yaml\snakeyaml\1.17\snakeyaml-1.17.jar;E:\mavenRepository\org\springframework\boot\spring-boot-starter-tomcat\1.5.10.RELEASE\spring-boot-starter-tomcat-1.5.10.RELEASE.jar;E:\mavenRepository\org\apache\tomcat\embed\tomcat-embed-core\8.5.27\tomcat-embed-core-8.5.27.jar;E:\mavenRepository\org\apache\tomcat\tomcat-annotations-api\8.5.27\tomcat-annotations-api-8.5.27.jar;E:\mavenRepository\org\apache\tomcat\embed\tomcat-embed-el\8.5.27\tomcat-embed-el-8.5.27.jar;E:\mavenRepository\org\apache\tomcat\embed\tomcat-embed-websocket\8.5.27\tomcat-embed-websocket-8.5.27.jar;E:\mavenRepository\org\hibernate\hibernate-validator\5.3.6.Final\hibernate-validator-5.3.6.Final.jar;E:\mavenRepository\javax\validation\validation-api\1.1.0.Final\validation-api-1.1.0.Final.jar;E:\mavenRepository\org\jboss\logging\jboss-logging\3.3.1.Final\jboss-logging-3.3.1.Final.jar;E:\mavenRepository\com\fasterxml\classmate\1.3.4\classmate-1.3.4.jar;E:\mavenRepository\com\fasterxml\jackson\core\jackson-databind\2.8.10\jackson-databind-2.8.10.jar;E:\mavenRepository\org\springframework\spring-web\4.3.14.RELEASE\spring-web-4.3.14.RELEASE.jar;E:\mavenRepository\org\springframework\spring-aop\4.3.14.RELEASE\spring-aop-4.3.14.RELEASE.jar;E:\mavenRepository\org\springframework\spring-beans\4.3.14.RELEASE\spring-beans-4.3.14.RELEASE.jar;E:\mavenRepository\org\springframework\spring-webmvc\4.3.14.RELEASE\spring-webmvc-4.3.14.RELEASE.jar;E:\mavenRepository\org\springframework\spring-expression\4.3.14.RELEASE\spring-expression-4.3.14.RELEASE.jar;E:\mavenRepository\org\springframework\cloud\spring-cloud-starter\1.3.1.RELEASE\spring-cloud-starter-1.3.1.RELEASE.jar;E:\mavenRepository\org\springframework\cloud\spring-cloud-context\1.3.1.RELEASE\spring-cloud-context-1.3.1.RELEASE.jar;E:\mavenRepository\org\springframework\security\spring-security-crypto\4.2.4.RELEASE\spring-security-crypto-4.2.4.RELEASE.jar;E:\mavenRepository\org\springframework\cloud\spring-cloud-commons\1.3.1.RELEASE\spring-cloud-commons-1.3.1.RELEASE.jar;E:\mavenRepository\org\springframework\security\spring-security-rsa\1.0.3.RELEASE\spring-security-rsa-1.0.3.RELEASE.jar;E:\mavenRepository\org\bouncycastle\bcpkix-jdk15on\1.55\bcpkix-jdk15on-1.55.jar;E:\mavenRepository\org\bouncycastle\bcprov-jdk15on\1.55\bcprov-jdk15on-1.55.jar;E:\mavenRepository\org\springframework\cloud\spring-cloud-netflix-core\1.4.2.RELEASE\spring-cloud-netflix-core-1.4.2.RELEASE.jar;E:\mavenRepository\org\springframework\cloud\spring-cloud-netflix-eureka-client\1.4.2.RELEASE\spring-cloud-netflix-eureka-client-1.4.2.RELEASE.jar;E:\mavenRepository\com\netflix\eureka\eureka-client\1.7.0\eureka-client-1.7.0.jar;E:\mavenRepository\org\codehaus\jettison\jettison\1.3.7\jettison-1.3.7.jar;E:\mavenRepository\stax\stax-api\1.0.1\stax-api-1.0.1.jar;E:\mavenRepository\com\netflix\netflix-commons\netflix-eventbus\0.3.0\netflix-eventbus-0.3.0.jar;E:\mavenRepository\com\netflix\netflix-commons\netflix-infix\0.3.0\netflix-infix-0.3.0.jar;E:\mavenRepository\commons-jxpath\commons-jxpath\1.3\commons-jxpath-1.3.jar;E:\mavenRepository\joda-time\joda-time\2.9.9\joda-time-2.9.9.jar;E:\mavenRepository\org\antlr\antlr-runtime\3.4\antlr-runtime-3.4.jar;E:\mavenRepository\org\antlr\stringtemplate\3.2.1\stringtemplate-3.2.1.jar;E:\mavenRepository\antlr\antlr\2.7.7\antlr-2.7.7.jar;E:\mavenRepository\com\google\code\gson\gson\2.8.2\gson-2.8.2.jar;E:\mavenRepository\org\apache\commons\commons-math\2.2\commons-math-2.2.jar;E:\mavenRepository\com\netflix\archaius\archaius-core\0.7.4\archaius-core-0.7.4.jar;E:\mavenRepository\javax\ws\rs\jsr311-api\1.1.1\jsr311-api-1.1.1.jar;E:\mavenRepository\com\netflix\servo\servo-core\0.10.1\servo-core-0.10.1.jar;E:\mavenRepository\com\netflix\servo\servo-internal\0.10.1\servo-internal-0.10.1.jar;E:\mavenRepository\com\sun\jersey\jersey-core\1.19.1\jersey-core-1.19.1.jar;E:\mavenRepository\com\sun\jersey\jersey-client\1.19.1\jersey-client-1.19.1.jar;E:\mavenRepository\com\sun\jersey\contribs\jersey-apache-client4\1.19.1\jersey-apache-client4-1.19.1.jar;E:\mavenRepository\org\apache\httpcomponents\httpclient\4.5.5\httpclient-4.5.5.jar;E:\mavenRepository\org\apache\httpcomponents\httpcore\4.4.9\httpcore-4.4.9.jar;E:\mavenRepository\commons-codec\commons-codec\1.10\commons-codec-1.10.jar;E:\mavenRepository\com\google\inject\guice\4.1.0\guice-4.1.0.jar;E:\mavenRepository\aopalliance\aopalliance\1.0\aopalliance-1.0.jar;E:\mavenRepository\com\fasterxml\jackson\core\jackson-annotations\2.8.0\jackson-annotations-2.8.0.jar;E:\mavenRepository\com\fasterxml\jackson\core\jackson-core\2.8.10\jackson-core-2.8.10.jar;E:\mavenRepository\com\netflix\eureka\eureka-core\1.7.0\eureka-core-1.7.0.jar;E:\mavenRepository\org\codehaus\woodstox\woodstox-core-asl\4.4.1\woodstox-core-asl-4.4.1.jar;E:\mavenRepository\javax\xml\stream\stax-api\1.0-2\stax-api-1.0-2.jar;E:\mavenRepository\org\codehaus\woodstox\stax2-api\3.1.4\stax2-api-3.1.4.jar;E:\mavenRepository\org\springframework\cloud\spring-cloud-starter-netflix-archaius\1.4.2.RELEASE\spring-cloud-starter-netflix-archaius-1.4.2.RELEASE.jar;E:\mavenRepository\commons-configuration\commons-configuration\1.8\commons-configuration-1.8.jar;E:\mavenRepository\commons-lang\commons-lang\2.6\commons-lang-2.6.jar;E:\mavenRepository\com\google\guava\guava\18.0\guava-18.0.jar;E:\mavenRepository\org\springframework\cloud\spring-cloud-starter-netflix-ribbon\1.4.2.RELEASE\spring-cloud-starter-netflix-ribbon-1.4.2.RELEASE.jar;E:\mavenRepository\com\netflix\ribbon\ribbon\2.2.4\ribbon-2.2.4.jar;E:\mavenRepository\com\netflix\ribbon\ribbon-transport\2.2.4\ribbon-transport-2.2.4.jar;E:\mavenRepository\io\reactivex\rxnetty-contexts\0.4.9\rxnetty-contexts-0.4.9.jar;E:\mavenRepository\io\reactivex\rxnetty-servo\0.4.9\rxnetty-servo-0.4.9.jar;E:\mavenRepository\com\netflix\hystrix\hystrix-core\1.5.12\hystrix-core-1.5.12.jar;E:\mavenRepository\org\hdrhistogram\HdrHistogram\2.1.9\HdrHistogram-2.1.9.jar;E:\mavenRepository\io\reactivex\rxnetty\0.4.9\rxnetty-0.4.9.jar;E:\mavenRepository\io\netty\netty-codec-http\4.0.27.Final\netty-codec-http-4.0.27.Final.jar;E:\mavenRepository\io\netty\netty-codec\4.0.27.Final\netty-codec-4.0.27.Final.jar;E:\mavenRepository\io\netty\netty-handler\4.0.27.Final\netty-handler-4.0.27.Final.jar;E:\mavenRepository\io\netty\netty-transport-native-epoll\4.0.27.Final\netty-transport-native-epoll-4.0.27.Final.jar;E:\mavenRepository\io\netty\netty-common\4.0.27.Final\netty-common-4.0.27.Final.jar;E:\mavenRepository\io\netty\netty-buffer\4.0.27.Final\netty-buffer-4.0.27.Final.jar;E:\mavenRepository\io\netty\netty-transport\4.0.27.Final\netty-transport-4.0.27.Final.jar;E:\mavenRepository\com\netflix\ribbon\ribbon-core\2.2.4\ribbon-core-2.2.4.jar;E:\mavenRepository\com\netflix\ribbon\ribbon-httpclient\2.2.4\ribbon-httpclient-2.2.4.jar;E:\mavenRepository\commons-collections\commons-collections\3.2.2\commons-collections-3.2.2.jar;E:\mavenRepository\com\netflix\netflix-commons\netflix-commons-util\0.1.1\netflix-commons-util-0.1.1.jar;E:\mavenRepository\com\netflix\ribbon\ribbon-loadbalancer\2.2.4\ribbon-loadbalancer-2.2.4.jar;E:\mavenRepository\com\netflix\netflix-commons\netflix-statistics\0.1.1\netflix-statistics-0.1.1.jar;E:\mavenRepository\io\reactivex\rxjava\1.2.0\rxjava-1.2.0.jar;E:\mavenRepository\com\netflix\ribbon\ribbon-eureka\2.2.4\ribbon-eureka-2.2.4.jar;E:\mavenRepository\com\thoughtworks\xstream\xstream\1.4.9\xstream-1.4.9.jar;E:\mavenRepository\xmlpull\xmlpull\1.1.3.1\xmlpull-1.1.3.1.jar;E:\mavenRepository\xpp3\xpp3_min\1.1.4c\xpp3_min-1.1.4c.jar;E:\mavenRepository\org\springframework\cloud\spring-cloud-starter-eureka-server\1.4.2.RELEASE\spring-cloud-starter-eureka-server-1.4.2.RELEASE.jar;E:\mavenRepository\org\springframework\cloud\spring-cloud-starter-netflix-eureka-server\1.4.2.RELEASE\spring-cloud-starter-netflix-eureka-server-1.4.2.RELEASE.jar;E:\mavenRepository\org\springframework\cloud\spring-cloud-netflix-eureka-server\1.4.2.RELEASE\spring-cloud-netflix-eureka-server-1.4.2.RELEASE.jar;E:\mavenRepository\org\springframework\boot\spring-boot-starter-actuator\1.5.10.RELEASE\spring-boot-starter-actuator-1.5.10.RELEASE.jar;E:\mavenRepository\org\springframework\boot\spring-boot-actuator\1.5.10.RELEASE\spring-boot-actuator-1.5.10.RELEASE.jar;E:\mavenRepository\org\springframework\boot\spring-boot-starter-freemarker\1.5.10.RELEASE\spring-boot-starter-freemarker-1.5.10.RELEASE.jar;E:\mavenRepository\org\freemarker\freemarker\2.3.27-incubating\freemarker-2.3.27-incubating.jar;E:\mavenRepository\org\springframework\spring-context-support\4.3.14.RELEASE\spring-context-support-4.3.14.RELEASE.jar;E:\mavenRepository\com\sun\jersey\jersey-servlet\1.19.1\jersey-servlet-1.19.1.jar;E:\mavenRepository\com\sun\jersey\jersey-server\1.19.1\jersey-server-1.19.1.jar;E:\mavenRepository\javax\inject\javax.inject\1\javax.inject-1.jar;E:\mavenRepository\com\fasterxml\jackson\dataformat\jackson-dataformat-xml\2.8.10\jackson-dataformat-xml-2.8.10.jar;E:\mavenRepository\com\fasterxml\jackson\module\jackson-module-jaxb-annotations\2.8.10\jackson-module-jaxb-annotations-2.8.10.jar;E:\mavenRepository\com\fasterxml\woodstox\woodstox-core\5.0.3\woodstox-core-5.0.3.jar;E:\mavenRepository\org\springframework\boot\spring-boot\1.5.10.RELEASE\spring-boot-1.5.10.RELEASE.jar;E:\mavenRepository\org\springframework\spring-context\4.3.14.RELEASE\spring-context-4.3.14.RELEASE.jar;E:\mavenRepository\org\springframework\boot\spring-boot-autoconfigure\1.5.10.RELEASE\spring-boot-autoconfigure-1.5.10.RELEASE.jar;E:\mavenRepository\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar;E:\mavenRepository\org\springframework\spring-core\4.3.14.RELEASE\spring-core-4.3.14.RELEASE.jar;E:\IntelliJIDEA181.3741.2\lib\idea_rt.jar"com.example.demo.aaa.TestConnectedtothetargetVM,address:'127.0.0.1:59784',transport:'socket'blockAblockBblockADisconnectedfromthetargetVM,address:'127.0.0.1:59784',transport:'socket'Processfinishedwithexitcode0
2 回答
Smart猫小萌
TA贡献1911条经验 获得超7个赞
其实{System.out.println("blockA");}这普通代码块在class文件里面是放在无参构造器里面的,有兴趣可以看一下class文件代码静态成员publicstaticTestt1=newTest();最先执行,创建一个对象调用到无参构造器,所以最先打印blockA。如果你的代码写上无参数构造器publicTest(){System.out.println("构造器");}那么在class文件里面是这样的。publicTest(){System.out.println("blockA");System.out.println("构造器");}这就是普通代码块比构造器先执行的原因。
添加回答
举报
0/150
提交
取消