One, the static member of the two kinds of calls.
1. Object. member variable = value;
Example: STU1. name = "Java";
2. Class name. Member mode; It can be called directly in this way without the need for new, and the Calling method can also (Student. Getnum ())
Example: Student. count++;
Note:
Static methods can only access static variables, and static can only access statics.
Non-static methods can access both static and non-static variables.
Example 1:class student{
static int count;
public int Getnum () {///non-static method can call a static variable or call a non-static variable
count++;
return count;
}
}
The main program call must be new to an object before it can be called (because the Getnum () method is non-static, so it is not shared, to be new out of the object, it can be called with an object)
Student stu1 = new Student ();
int count1 = Stu1.getnum ();
Example 2:class student{
static int count;
public static int Getnum () {
count++;
return count;
}
}
The main program calls int count2 = Student.getnum ();
Second, static block.
static{
}
Static blocks can only be loaded and run once, and executed first.
Example: Class student{
public static int count;
static{
count++;
}
public void Studentnum () {
System.out.println (count);
}
}
Main program Call: New Student (). Studentnum (); 1
New Student (). Studentnum (); 1
Three, encapsulation.
1. Encapsulate the function of the code:
Provide only a name to the outside world to allow access to these properties or functions.
2. Example: Bank security
Class creditcard{
private double salary; Outside access to the salary, it encapsulates
Public double getsalary () {
return salary;
}
public void Setsalary (double sal) {
if (Sal <= 0) {
return 0.0;
}else{
salary = sal;
}
return salary;
}
}
public class money{
public static void Main (string[] args) {
CreditCard cc = new CreditCard ();
Cc.setsalary (100000000);
Double salary = cc.getsalary ();
}
}
Note: Private privately, only own, outside access.
Java Foundation Chapter eighth (invocation of static members, encapsulation)