Java variable type, instance variable and local variable static variable

Source: Internet
Author: User
Tags modifiers visibility


Instance variable:
  • An instance variable is declared in a class, but outside of the method, a constructor or any block.
  • When space is allocated to an object in the heap, the slots create values for each instance variable.

  • When an object is created with the use of the keyword "new", the object is destroyed destroying the created instance variable.

  • An instance variable is considered to be a value that must be referenced by more than one method, a constructor or block, or a critical part of an object's state must appear in the entire class.

  • Instance variables can be declared at levels before or after use.

  • An access modifier can give an instance variable.

  • Instance variables for all methods, constructors and blocks are visible in the class. Generally, it is recommended to make these variables private (access layer). However visibility subclasses can give these variables with the use of access modifiers.

  • The instance variable has a default value. The default value for a number is 0, and the Boolean value is false and the object reference is null. Values can be assigned in declarations or constructors.

  • Instance variables can be accessed directly through the class that invokes the variable name. However, in static methods and different classes (when an instance variable is given access) the objectreference.variablenameshould be called with the fully qualified name.

  • 1 public class Sample
    2 {
    3 private int a = 1; / / instance variable
    Four
    5  public void b()
    6 {
    7 int a = 2; / / local variable
    8 system. Out. Println ("local variable: a =" + a);
    9 system. Out. Println ("instance variable: a =" + this. A); / / reference instance variable within the scope of local variable: this. Variable name
    10}
    11  public static void main(String[] args)
    12 {
    13   new Sample().b();
    14}
    15}
    Sixteen 


    =========================================================================================





Local variables:
    • Local variables are declared in a method, constructor, or block.

    • When you create a method for a local variable, the constructor or block is entered, and once you exit the method, the constructor or the variable in the block is destroyed.

    • Access modifiers cannot be used with local variables.

    • Local variables are visible only within declared methods, constructors, or blocks.

    • Local variables are implemented internally at the stack level.

    • There is no default value for local variables here, so local variables should be declared and the initial values should be allocated before the first use.

For example:


Here, age is a local variable. This is the definition inside the pupage () method, whose scope is limited to that method.


 
 
1 public class Test{ 
 2    public void pupAge(){
 3       int age = 0;
 4       age = age + 7;
 5       System.out.println("Puppy age is : " + age);
 6    }
 7    
 8    public static void main(String args[]){
 9       Test test = new Test();
10       test.pupAge();
11    }
12 }





This will produce the following results:


Puppy Age Is:7




For example:


The following example uses age not to initialize it, so it will give an error message at compile time.


 
 
1 public class Test{ 
 2    public void pupAge(){
 3       int age;
 4       age = age + 7;
 5       System.out.println("Puppy age is : " + age);
 6    }
 7    
 8    public static void main(String args[]){
 9       Test test = new Test();
10       test.pupAge();
11    }
12 }





Compile it, which will produce the following error:


Test.java:4= age + 7;  ^1 Error







class/Static variables:
    • A class variable is also known as a static variable declared in the Static keyword of the class, but outside of the method, a constructor or a block.

    • Each class variable has only one copy, regardless of how many objects are created from it.

    • Static variables are seldom used and are not declared as constants. Constants are declared as public/private, final and static variables. Constant variables never change from their initial values.

    • Static variables are stored in static memory. It is rare to use static variables other than declared as final, as a public or private constant.

    • Static variables are created when the program starts, and the program stops destroying.

    • Visibility is similar to instance variables. However, most static variables are declared public because they must be available to the consumer of the class.

    • The default value is the same instance variable. For a number, the default value is 0, the Boolean value, which is false, and the object reference, which is null. Values can be assigned in declarations or constructors. Additional values can be assigned in a special static initialization block.

    • Static variables can be accessed by invoking the class name. Classname.variablename.

    • When a variable is defined as public static final , the name (constant) of the variable is uppercase. If the static variable is not exposed and the final naming syntax is the same as the instance variable and the local variable.

Example:
 
 1 import java.io.*;
 2 
 3 public class Employee{
 4    // salary  variable is a private static variable
 5    private static double salary;
 6 
 7    // DEPARTMENT is a constant
 8    public static final String DEPARTMENT = "Development ";
 9 
10    public static void main(String args[]){
11       salary = 1000;
12       System.out.println(DEPARTMENT+"average salary:"+salary);
13    }
14 }





This will produce the following results:


Development average salary:1000





Note: If a variable is accessed from an external class, the constant should be accessed employee.department



Java variable type, instance variable and local variable static variable


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.