The static domain in the Java "Tij" validation class has only one storage space, regardless of the number of objects instantiated by the class.

Source: Internet
Author: User

Static purpose: Defining a statically member, static method

static feature : Belongs to class; No matter how many objects are instantiated, the static domain occupies only one memory

Call method : Call directly with class name or object instantiated by class


A static field in a class consists of two kinds, one is a static member and one is a static method. They are all belong to the class, not belong to

of an object. They can be called directly with the class name.

Of course, you can also use objects to invoke. But from the beginning to the end, they only exist in one memory. Static methods can be used in classes where the other

Static methods and static members, non-static methods and Non-static members cannot be invoked, and non-static methods can be arbitrarily invoked without restriction.


* * Verify the static domain in the class no matter how many objects are created, there is only one instance of the static field: W
*thinking in Java Test 2.8
* @author: Wolfofsiberian
*
* * Public
class tij_test2_8{
	static int a=0;
	
	Public Tij_test2_8 () {
		a++;
		System.out.println ("a=" +a);
	}
	public static void Main (String args[]) {
		tij_test2_8 test1=new tij_test2_8 ();
		System.out.println ("test1.a=" +test1.a);

		Tij_test2_8 test2=new tij_test2_8 ();
		System.out.println ("test2.a=" +test2.a);

		Tij_test2_8 test3=new tij_test2_8 ();
		System.out.println ("test3.a=" +test3.a);

		Tij_test2_8 test4=new tij_test2_8 ();
		System.out.println ("test4.a=" +test4.a);

		Tij_test2_8 test5=new tij_test2_8 ();
		System.out.println ("test5.a=" +test5.a);
		
		tij_test2_8.a=100;

		System.out.println ("test1.a=" +test1.a);
		System.out.println ("test2.a=" +test2.a);
		System.out.println ("test3.a=" +test3.a);
		System.out.println ("test4.a=" +test4.a);
		System.out.println ("test5.a=" +test5.a);
	}


The results of the execution are as follows, and the modification of member A in the constructor is reflected in each newly created object. Use a class to call a, modify its value,

The modified values are reflected in all of the objects.


Test1.a=1
test2.a=2
Test3.a=3
Test4.a=4
Test5.a=5
test1.a=100
test2.a=100
test3.a=100
test4.a=100
test5.a=100


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.