[go: nahoru, domu]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于 “在一个静态方法内调用一个非静态成员为什么是非法的” 一点个人理解 #1126

Open
daochen23 opened this issue Mar 13, 2021 · 4 comments
Labels
discuss discuss a problem

Comments

@daochen23
Copy link

问题

为什么在静态方法内调动一个非静态的成员是非法的?

我的理解

  • 首先,static 修饰的成员变量和方法是属于类的,它随着类的加载而加载,随着类的消失而消失
  • 而那些非 static 修饰的普通成员变量是属于类的实例的,也就是对象的,它们只有在类的实例化之后才会被加载
  • 所以静态的成员变量和静态方法比非静态的成员变量和方法优先加载

总结

一个优先加载的去调用一个还没有加载的当然是非法的了

@zaiweijian
Copy link

还有其他理解吗?

@daochen23
Copy link
Author

还有其他理解吗?

这是我的一点理解,更深层的估计就要翻源码了

@61go
Copy link
61go commented Mar 24, 2021 via email

@charlienss
Copy link
charlienss commented Apr 17, 2021

同意楼上,另外补充一点:字符串常量池还在堆里,只不过运行时常量在元空间。

@Snailclimb Snailclimb added the discuss discuss a problem label Apr 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discuss discuss a problem
Projects
None yet
Development

No branches or pull requests

6 participants
@zaiweijian @Snailclimb @charlienss @61go @daochen23 and others