JAVA global variables and local variables

Source: Internet
Author: User

JAVA global variables and local variables are similar in usage and nature in php and asp.net. I will introduce JAVA global variables and local variables in detail.

Global variables:It is also called a member variable, which is a variable defined in the class and is valid throughout the class. Global variables can also be divided into class variables and instance variables.

1. class variable: static variable. It can be called directly by class name or object, in addition, all the same class variables of the object share the same memory space.

Example 1
 

The Code is as follows: Copy code

Tst. java, no main () method
Package com. blogchina. qb2049;
Public class Tst
{
Static {System. out. println ("111111 ");}
}

Running result: 111111
Exception in thread "main" java. lang. NoSuchMethodError: main


Static final variable, which must be initialized at the time of declaration or in the static block.

So if you want to use program input as its variable, you must modify the variable type.
You can write

The Code is as follows: Copy code

Public class xx
{
Public final String PackageName;
Public xx (String name)
{
PackageName = name;
}
}

Define a xx static instance public static xx xxInstance = new xx ("input value ");
When calling, it will write xxInstance. PackageName

2. instance variables:Without static modification, it can only be called through objects, and the same instance variables of all objects share different inner storage spaces.
 

Local variables:It refers to the variables and method parameters defined in the method body. It is valid only in the method that defines it.

Let's take a look at the following two procedures:

The Code is as follows: Copy code

Procedure 1:
Public class Variable
{
Int I;
Void test ()
{
Int j = 8;
If (j = I)
System. out. println ("Equal ");
Else
System. out. println ("not equal ");
}
Public static void main (String [] args)
{
Variable v = new Variable ();
V. test ();
}
}


Procedure 2:
Public class Variable
{
Void test ()
{
Int I;
Int j = 8;
If (j = I)
System. out. println ("Equal ");
Else
System. out. println ("not equal ");
}
Public static void main (String [] args)
{
Variable v = new Variable ();
V. test ();
}
}


The first program is normal and there will be no errors during compilation. The second program will prompt the following error during compilation:

D: Programjavatest> javac Variable. java
Variable. java: 9: Variable I may not be initialized
If (j = I)
^
1 error

This error occurs because the member variable has a default value. (if it is modified by final and has no static value, it must be explicitly assigned .)


Note: If the name of a local variable in a method is the same as that of a global variable, the global variable is temporarily invalid in this method.

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.