Differences between Java local variables, instance variables, class variables (static variables)

Source: Internet
Author: User

1. Local Variables:

A local variable is a variable in a method of a class;

2. Instance variables:

Instance variables are also variables that are independent of the method in the class, but do not have static adornments, also called object variables

3. Class variables (static variables):

A class variable is a variable that is independent of a method in a class, and is statically decorated, also called a static variable

4. Pseudo-code Description
public class Variable{     static int allClicks=0;//类变量 String str="hello world";//实例变量 public void method(){ int i =0;//局部变量 }}
5. Instance variables differ from class variables

The difference is:
The difference between a class variable and an instance variable is that the class variable is common to all objects, where one object changes its value, the other object gets the changed result, and the instance variable is the object's private, and an object changes its value without affecting other objects;

Demo Description:

PublicClass a{Staticint a =0;//class variable private int B = 0; //instance variable} public class b{public void main (string[] args) {A a1 = new A (); A a2 = new A (); a1.a = 3; //equals a.a = 3; a1.b = 4; System. out.println (a2.a); //result is 3 //class variable is for all objects, so A1 change A,a2 's a also change System.out.println (a2.b); //result is 0 //instance only changes itself, so the A1 object B changes, does not affect the object a2 b variable}} 
/span>

Valid range of variables
Member variables: Declared in a class, valid in the entire class {
Static variables use the static keyword to decorate public static int age=100; You can use the class's instance object access or you can access it directly using the class name
Instance variables can only be accessed through instance objects


}
Local variables: Variables declared in a code block within a method or within a method (inside a method, "{" and "}")
A local variable declared in a code block within a method, only valid in the current code block
Local variables declared within a method outside the code block are valid throughout the method
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

Differences between Java local variables, instance variables, class variables (static variables)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.