当前位置:网站首页>The difference between static and instance variables

The difference between static and instance variables

2020-12-08 12:00:59 osc_ 1pgfn7k9

Static variables and instance variables ( Method ) The difference between

This question is mainly about our understanding of static How to understand static keywords ( It is divided into the following three points

One 、 Grammatical differences

Add before static variable static keyword , Examples don't need

Two 、 Subordination distinction

Instance variable belongs to an object's property . Static belongs to the class ( When using static variables, use the class name directly . Static variables are OK ) It's different in use .

3、 ... and 、 Operational differences ( The fundamental difference )

Static variables are in JVM When loading this class , It's created , And in the process of running , Static variables cannot be released by garbage collection . Instance variables are created when an object is instantiated , That's execution new When he started to create , For instance variables , When we don't use it anymore , It will be recycled by us

Be careful :

Static variables and instance variables are stored in different places , Static variables are stored in JVM In the method area of , Instance variables exist in the memory of the object heap . When it comes to actual development , Static variables have relatively small storage space , And instance variables have more storage space , In us Java In object oriented programming , In most scenarios, we recommend using instance variables first and reducing static variables

版权声明
本文为[osc_ 1pgfn7k9]所创,转载请带上原文链接,感谢
https://chowdera.com/2020/12/202012081200521490.html