Java in the understanding of static

Author:Anonymous    Updated:2008-10-26 21:31:07
Static variables: types of variables, such as the sharing of all objects
Static: static method is not used this
In the static method can not access a member of the non-static variables and methods
Direct access through the static members of the category, even if there is no such object
/ / Student.java

package cn.edu.uibe.oop;

public class Student (
String name; / / Name of student
static int counter = 0; / / The number of students object

public Student (String name) (
this.name = name;
counter + +; / / object count 1, the need for static variables can all share target
)
public void print () (
System.out.println ( "name =" + name + "\ tcounter =" + counter);
)
public static void showCounter () (
System.out.println ( "counter =" + counter);
/ / System.out.println (name); / / error, a static method which do not have access to the members of the non-static variables and methods
/ / this. / / error, there is no static approach this quote
)

public static void main (String [] args) (
Student [] student = (
new Student ( "zhangsan"),
new Student ( "lisi"),
new Student ( "zhaowu")
);
for (int i = 0; i <student.length; i + +) (
student [i]. print ();
)
new Student ( "wuming");
System.out.println ( "counter =" + Student.counter);
Student.showCounter ();

)

)
Previous:NO
Next:File papers to create an example
User Reviews
Site Search
Recommended article
AD