码迷,mamicode.com
首页 > 编程语言 > 详细

Summary: Java Grammar

时间:2014-11-22 13:11:35      阅读:247      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   ar   color   os   sp   java   on   

这里列举的是一些我平时碰到的一些Java Grammar,日积月累。

Class Variable vs Instance Variable: 

Instance variables

Instance variable is the variable declared inside a class, but outside a method Instance variables belong to an instance of a
class. Another way of saying that is instance variables belong to an object, since an object is an instance of a class. Every object has it’s own copy of the instance variables. Here is what a declaration of an instance variable would look like: Example of an instance variable: class Taxes { int count; /*...*/ }
Class Variable

Class variables, however, only have one copy of the variable(s) shared with all instances of the class. It’s important to remember that class variables are also known as static member variables in C++, Java, and C#. Each object of the class does not have its own copy of a class variable. Instead, every object shares the one and only copy of that class variable – and any changes made to that copy are seen by all of the objects of that class. Here is what a class variable – or a static member variable – would look like in C++:

Example of a class variable:

class Taxes
{
  static int count;
  /*...*/
}
Difference between class and instance variables

Now, it should be clear what the difference between instance and class variables is. Class variables only have one copy that is shared by all the different objects of a class, whereas every object has it’s own personal copy of an instance variable. So, instance variables across different objects can have different values whereas class variables across different objects can have only one value.

 

Summary: Java Grammar

标签:style   blog   io   ar   color   os   sp   java   on   

原文地址:http://www.cnblogs.com/EdwardLiu/p/4114979.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!