Classes and Objects (ii)

Source: Internet
Author: User
static code blocks are executed only once, when the Java Virtual machine loads the class, and if the class contains more than one static block, then executes from top to down
One: Static variables
What is a static variable A state variable is a variable that is modified by static a static variable belongs to a class and is shared by all instances can be accessed through the class name or through the instance name using static variables

Static type variable name

Class Monkey {//Declaration of static variable statics
	int count=0;
}
Class Test {public
	static void Main (string[] args) {for
		(int i=0;i<3;i++) {
			monkey m=new monkey (); 
  m.count=m.count+1;
		}
		Access static variable
		System.out.println (monkey.count) by class name;

the difference between a static variable and a non-static variable
Class Saticvar {
		//instance variable
		int a=1;
		Statically variable static
		int b=1;
}


Class Test {public
	static void Main (string[] args) {
		staticvar s1=new staticvar ();
		s1.a++;
		s1.b++;
		SYSTEM.OUT.PRINTLN ("instance variable a=" + s1.a);
		SYSTEM.OUT.PRINTLN ("Static variable b=" +staticvar.b);
		Staticvar s2=new Staticvar ();
		s2.a++;
		s2.b++;
		SYSTEM.OUT.PRINTLN ("instance variable a=" + s2.a);
		SYSTEM.OUT.PRINTLN ("Static variable b=" + s2.b);
	}	


static variables in memory only one memory space, in the process of loading the class to complete the memory allocation of static variables can be directly accessed through the class name An instance variable allocates memory for instance variables every time an instance is created, and each object accesses its own variables. a static variable can be accessed by a class name or instance object, and an instance variable can only be accessed by an instance object : static method static method: A method that is decorated by static is called a static method or a class method

static[modifier] Returns the value type method name (type parameter 1, type parameter 2 ...) {

Method body

}

Class Monkey {
	//static method static
	void Play () {
		System.out.println () plays divine. ");
	}	
}
Class Test {public
	static void Main (string[] args) {
		//Sample 10 objects for
		(int i=1;i<=10;i++) {
			Monkey Houge=new Monkey ();
			Call static method
			Monkey.play () by class name ();}}	


<p><span style= "Color:black;" > Static methods can only access static variables and cannot access instance variables, because when static methods are loaded, instance variables </span></p><p><span style= "color:black;" > has not allocated memory space yet. </span></p>
Three: Static code block A static code block does not exist in any method body.
Class Staticblock {
		static{
		   System.out.println ("I am a static code block");
		}

The static code block is only executed once, and it executes when the Java Virtual machine loads the class.

If the class contains more than one static block, execute the order of the different parts of the class from the top down order

Class Order {
		int a=1;
		static int b=1;
		{
			System.out.println ("instance variable a=" +a);
			System.out.println ("code block execution.") ");
		}
		static {
			System.out.println ("Static variable b=" +b);
			System.out.println ("Static code block execution.") ");
		}
		Order () {
		 System.out.println ("construct method Execution"); 	
		}

<pre class= "java" name= "code" >class Test {public
		static void Main (string[] args) {Order
			order=new order ();
		}	
}
Compile and draw the conclusion:
static variable initialization → static code block → initialization static method → Initialization instance variable → code block → construction method Four: Constants life cycle of variables There are 3 kinds of variables in Java, namely class variables, instance variables, and temporary local variables defined in a method. Different variables have different life cycles
Class Cycle {
	 int var1 = 1;
	 static int var2 = 2;
	 public int Add () {
	 	int var3 = var1 + var2;
	 	Return VAR3
	 }
}
Class Test {public
	static void Main (string[] args) {
		Cycle c=new Cycle ();
       C.add ();
	}	

var 1,2,3, respectively, represents
instance variables, static variables, local variables

Final variable final variable
Class Finalvar {
	final int max_value=20;
	final int min_value;
	max_value=30;
	Static final  double pi=3.1415926;
	pi=3.1415;
}

final int max_value=20; right.

Final int Min_value, must be initialized at error definition

MAX_VALUE=30 error, cannot repeat assignment

Staticfinal Double pi=3.1415926 Correct

pi=3.1415 error Static instance variable cannot be repeated assignment using the final-decorated variable to indicate that the value does not change the value of the final decorated instance variable cannot be changed, which means that when instantiating multiple objects with final variables, Each object will have the same final variable value. This instance variable, instead of using static variables, is more consistent with the design rules, and therefore often defines the final variable as static (static final). The final variable's feature final modifier can modify static variables, instance variables, and local variables to represent static constants, instance constants, and local constant final-type variables that must be explicitly initialized or cause compilation errors final variable can only be assigned once when the final constant is defined, it is generally named with an uppercase letter, and multiple words are separated by a "_" Sign : A resource file in which the same name cannot be placed in a folder , Java is also the case, went to a better and more reasonable to allocate resources, so the package appeared. java commonly used packages , which are provided by the JDK

Java.lang Packages: System classes (Systems), integer classes (integers), and string classes (String)

java.io Package: File input Stream Class (FileInputStream Class), file output stream class (FileOutputStream), such as N java.util Package: Provide date class (date), collection Class (Collection) and other practical classes If a class accesses a class from another package (except Java.lang), the class must first be introduced through the import statement

Import Com.xxx.edu.server.Teacher;

In a Java source file, only one package statement is allowed and must be located in the first sentence of the source file

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.