class Person{ // 定义Person类
String name ; // 定义name属性,暂时不封装
int age ; // 定义age属性,暂时不封装
String country = "A城" ; // 定义城市属性,有默认值
public Person(String name,int age){
this.name = name ;
this.age = age;
}
public void info(){ // 得到信息
System.out.println("姓名:" + this.name + ",年龄:" + this.age + ",城市:" + country) ;
}
};
public class StaticDemo01{
public static void main(String args[]){
Person p1 = new Person("张三",30) ; // 实例化对象
Person p2 = new Person("李四",31) ; // 实例化对象
Person p3 = new Person("王五",32) ; // 实例化对象
p1.info() ;
p2.info() ;
p3.info() ;
}
};class Person{ // 定义Person类
String name ; // 定义name属性,暂时不封装
int age ; // 定义age属性,暂时不封装
static String country = "A城" ; // 定义城市属性,有默认值,static
public Person(String name,int age){
this.name = name ;
this.age = age;
}
public void info(){ // 得到信息
System.out.println("姓名:" + this.name + ",年龄:" + this.age + ",城市:" + country) ;
}
};
public class StaticDemo02{
public static void main(String args[]){
Person p1 = new Person("张三",30) ; // 实例化对象
Person p2 = new Person("李四",31) ; // 实例化对象
Person p3 = new Person("王五",32) ; // 实例化对象
System.out.println("--------------- 修改之前 -------------") ;
p1.info() ;
p2.info() ;
p3.info() ;
p1.country = "B城" ; // 修改static属性
System.out.println("--------------- 修改之后 -------------") ;
p1.info() ;
p2.info() ;
p3.info() ;
}
};class Person{ // 定义Person类
private String name ; // 定义name属性,暂时不封装
private int age ; // 定义age属性,暂时不封装
private static String country = "A城" ; // 定义城市属性,有默认值,static
public static void setCountry(String c){ // 此方法可以直接由类名称调用
country = c ;
}
public static String getCountry(){
return country ;
}
public Person(String name,int age){
this.name = name ;
this.age = age;
}
public void info(){ // 得到信息
System.out.println("姓名:" + this.name + ",年龄:" + this.age + ",城市:" + country) ;
}
};
public class StaticDemo04{
public static void main(String args[]){
Person p1 = new Person("张三",30) ; // 实例化对象
Person p2 = new Person("李四",31) ; // 实例化对象
Person p3 = new Person("王五",32) ; // 实例化对象
System.out.println("--------------- 修改之前 -------------") ;
p1.info() ;
p2.info() ;
p3.info() ;
Person.setCountry("B城") ; // 调用静态方法修改static属性的内容
System.out.println("--------------- 修改之后 -------------") ;
p1.info() ;
p2.info() ;
p3.info() ;
}
};class Person{ // 定义Person类
private static String country = "A城" ; // 定义静态属性
private String name = "Hello" ;
public static void sFun(String c){
System.out.println("name = " + name) ; // 错误,不能调用非static属性
fun() ; // 错误,不能调用非static方法
}
public void fun(){
System.out.println("World") ;
}
};
class Demo{ // 定义Person类
private static int count = 0 ; // 所有对象共享此属性
public Demo(){
count++ ; // 只要有对象产生就应该自增
System.out.println("产生了" + count + "个对象!") ;
}
};
public class StaticDemo06{
public static void main(String args[]){
new Demo() ; // 增加新对象
new Demo() ; // 增加新对象
new Demo() ; // 增加新对象
}
};class Demo{ // 定义Person类
private String name ; // 保存名字
private static int count = 0 ; // 所有对象共享此属性
public Demo(){
count++ ; // 有对象产生就自增
this.name = "DEMO-" + count ; // 自动进行编名操作
}
public Demo(String name){
this.name = name; // 可以通过构造赋值
}
public String getName(){ // 取得姓名
return this.name ;
}
};
public class StaticDemo07{
public static void main(String args[]){
System.out.println(new Demo().getName()) ;
System.out.println(new Demo("LXH").getName()) ;
System.out.println(new Demo().getName()) ;
System.out.println(new Demo("MLDN").getName()) ;
System.out.println(new Demo().getName()) ;
}
};public class CodeDemo01{
public static void main(String args[]){
{ // 普通代码块
int x = 30 ; // 就属于一个局部变量
System.out.println("普通代码块 --> x = " + x) ;
}
int x = 100 ; // 与局部变量名称相同
System.out.println("代码块之外 --> x = " + x) ;
}
};class Demo{
{ // 直接在类中编写代码块,称为构造块
System.out.println("1、构造块。") ;
}
public Demo(){ // 定义构造方法
System.out.println("2、构造方法。") ;
}
};
public class CodeDemo02{
public static void main(String args[]){
new Demo() ; // 实例化对象
new Demo() ; // 实例化对象
new Demo() ; // 实例化对象
}
};class Demo{
{ // 直接在类中编写代码块,称为构造块
System.out.println("1、构造块。") ;
}
static{ // 使用static,称为静态代码块
System.out.println("0、静态代码块") ;
}
public Demo(){ // 定义构造方法
System.out.println("2、构造方法。") ;
}
};
public class CodeDemo03{
static{ // 在主方法所在的类中定义静态块
System.out.println("在主方法所在类中定义的代码块") ;
}
public static void main(String args[]){
new Demo() ; // 实例化对象
new Demo() ; // 实例化对象
new Demo() ; // 实例化对象
}
};public class CodeDemo04{
static{
System.out.println("Hello World!!!") ;
System.exit(1) ; // 程序退出
}
};原文:http://blog.csdn.net/u013087513/article/details/50992879