对象当然可以调用静态成员了,修饰成static 也可以用类名直接访问,因为他在类加载时候就已经存在于方法区,
静态修饰的成员变量只要不是被private修饰的就能直接访问,并赋值且修改,- class Person
- {
- int age;
- String name;
- static String country = "cn";
- Person(int age,String name)
- {
- this.age = age;
- this.name = name;
- }
- public void speak()
- {
- System.out.println("我叫"+this.name+",今年"+this.age+"岁,"+"我的国籍是"+country);
- }
- }
- class Temp2
- {
- public static void main(String[] args)
- {
- Person p = new Person(20,"小五");
- p.country = "usa";
- p.speak();
- }
- }
复制代码 |