黑马程序员-java中静态变量,方法的使用实例
2012-03-22 19:25
387 查看
---------------------- android培训、java培训、期待与您交流! ----------------------
java中静态变量,方法,类的使用实例
class StaticTest
{
static int i=1;
static
{//静态区域块,静态区域块只能执行一次
i++;
}
public StaticTest()
{
i++;
}
public static void main(String[] args)
{
Static s1=new Static();
System.out.println(s1.i);
Static s2=new Static();
System.out.println(s2.i);
Student stu1=new Student(15,"张三",250);
Student stu2=new Student(16,"李四",260);
System.out.println(Student.getTotalFee());
}
}
/**
*增加学生类
*/
class Student
{
int age;
String name;
int fee;
static int totalFee;
public Student(int age,String name,int fee)
{
this.age=age;
this.name=name;
this.fee=fee;
totalFee+=fee;
}
//这是一个静态方法,即类方法,所有的对象都共享一个方法,节省栈的开销
public static int getTotalFee()
{
return totalFee;
}
}
---------------------- android培训、java培训、期待与您交流! ----------------------详细请查看:http://edu.csdn.net/heima
java中静态变量,方法,类的使用实例
class StaticTest
{
static int i=1;
static
{//静态区域块,静态区域块只能执行一次
i++;
}
public StaticTest()
{
i++;
}
public static void main(String[] args)
{
Static s1=new Static();
System.out.println(s1.i);
Static s2=new Static();
System.out.println(s2.i);
Student stu1=new Student(15,"张三",250);
Student stu2=new Student(16,"李四",260);
System.out.println(Student.getTotalFee());
}
}
/**
*增加学生类
*/
class Student
{
int age;
String name;
int fee;
static int totalFee;
public Student(int age,String name,int fee)
{
this.age=age;
this.name=name;
this.fee=fee;
totalFee+=fee;
}
//这是一个静态方法,即类方法,所有的对象都共享一个方法,节省栈的开销
public static int getTotalFee()
{
return totalFee;
}
}
---------------------- android培训、java培训、期待与您交流! ----------------------详细请查看:http://edu.csdn.net/heima
相关文章推荐
- java 程序加载过程---3--类中申明同时申明类的静态对象 创建类的实例 访问类的静态变量 调用类的静态方法 使用反射方法 初始化类的子类对象 直接使用java.exe 调用某个类
- java序列化和serialVersionUID的使用方法实例
- java 静态变量与方法实例变量与方法的内存占用
- Java 的swing.GroupLayout布局管理器的使用方法和实例
- Java 的swing.GroupLayout布局管理器的使用方法和实例【图文说明】
- java===java基础学习(13)---this,static(静态变量和静态方法)的使用
- java的Console类的使用方法及实例
- Java 的swing.GroupLayout布局管理器的使用方法和实例【图文说明】
- 黑马程序员--Java基础加强--15.利用反射操作泛型IV【通过反射Method解析泛型方法思路】【通过Method对四种Type子接口类型进行解剖】【使用递归对任意复合泛型类型进行彻底解剖】【个人
- java修改文件名-renameTo()方法的使用实例,复制一个文件或者叫转存一个文件
- (转)Java 的swing.GroupLayout布局管理器的使用方法和实例
- 使用java反射中的getMethod,invoke方法调用对象方法的实例
- 使用Rhino让java执行javascript的方法实例
- Java Swing 非常漂亮外观Nimbus的使用方法实例
- Java wait() notify()方法使用实例讲解
- java中自带时间类使用方法实例 Date,Timestamp,DateFormat
- Java GroupLayout使用方法和实例
- java集合之ArrayList方法的使用实例
- java中get方法,和set方法的使用实例
- Java中的runnable方法的使用实例