1.static关键字
class Person{
private int age;
private String name;
private Person(int age,String name){
this.name=name;
this.age=age;
}
void speak(){
System.out.println("name:"+name+",age:"+age);
}
void sleep(){
System.out.println("睡觉。。");
}
}
class PersonDome{
public static void main(String[] args) {
Person p=new Person("张三", 23);
p.speak();
p.sleep();
}
}
2.静态方法的使用注意事项
class Person{
private int age;
private String name;
private Person(int age,String name){
this.name=name;
this.age=age;
}
void speak(){
System.out.println("name:"+name+",age:"+age);
}
//没有访问对象特有数据,可以声明为静态
static void sleep(){
System.out.println("睡觉。。");
}
}
class PersonDome{
public static void main(String[] args) {
Person.sleep();//静态方法,属于类的方法,可以通过类直接调用
}
}
![在这里
3.静态变量
class Circle{
private double radius;
static double pi=3.14;//圆周率每个圆共享,故声明为静态
Circle(double radius){
this.radius=radius;
}
double getArea(){
return radius*radius*pi;
}
}
class CircleDome{
public static void main(String[] args) {
System.out.println(new Circle(3).getArea());
}
}
4.静态变量和成员变量的区别
5.静态加载的内存图解
public class Person{
private String name;
private static String country="CN";
Person(String name){
this.name=name;
}
void showName(){
System.out.println("name="+name);
}
static void showCountry(){
System.out.println("country="+country);
}
}
class StaticDome{
public static void main(String[] args) {
Person p =new Person("haobobo");
p.showName();
Person.showCountry();
}
}
6.代码块
7.对象的创建过程
8.单例模式