package com.jk.ref;
class Ref{
int temp=10;
}
public class RefDemo01 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Ref re=new Ref();
re.temp=20;
System.out.println(re.temp);
Ref1(re);
System.out.println(re.temp);
}
public static void Ref1(Ref t){
t.temp=30;
}
}
package com.jk.ref;
public class RefDemo02 {
public static void main(String[] args) {
// TODO Auto-generated method stub
String st1="hello";
System.out.println(st1);
tell(st1);
System.out.println(st1);
}
public static void tell(String st2){
st2="world";
}
}
package com.jk.ref;
class Person{
private String name;
private int age;
public Person(String name,int age){
this();
this.name=name;
this.age=age;
}
public Person(){
System.out.println("无参数构造方法");
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the age
*/
public int getAge() {
return age;
}
/**
* @param age the age to set
*/
public void setAge(int age) {
this.age = age;
}
public void tell(){
System.out.println("name:"+getName()+" "+"age:"+getAge());
}
}
public class ThisDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
Person per=new Person("zhangsnan",20);
per.tell();
}
}
package com.jk.ref;
class Person{
public void tell(){
System.out.println(this);
}
}
public class ThisDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
Person per=new Person();
System.out.println(per);
per.tell();
}
}
java static关键字
1、使用static声明属性
static声明全局属性
2、使用static声明方法
直接通过类名调用
3、注意点
使用static方法的时候,只能访问static声明的属性和方法,而非static声明的属性和方法是不能访问的。
package com.jk.ref;
class People{
String name;
private static String country="中国";
public People(String name){
this.name=name;
}
public void tell(){
System.out.println("name:"+name+" "+"country:"+country);
}
/**
* @return the country
*/
public static String getCountry() {
return country;
}
/**
* @param country the country to set
*/
public static void setCountry(String country) {
People.country = country;
}
}
public class StaticDemo01 {
public static void main(String[] args) {
// TODO Auto-generated method stub
People.setCountry("shanghai");
People ps1=new People("zhangsan");
//People.country="上海";
ps1.tell();
People ps2=new People("lisi");
// ps2.country="上海";
ps2.tell();
People ps3=new People("wangwu");
// ps3.country="上海";
ps3.tell();
}
}
使用static方法的时候,只能访问static声明的属性和方法,而非static声明的属性和方法是不能访问的。
package com.jk.ref;
public class StaticDemo02 {
private static int i=10;
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(i);
tell();
}
public static void tell(){
System.out.println("hello");
}
}