一个类的主要构成就是:属性和方法,方法包括构造方法和普通方法,每一个对象都分别拥有各自的属性内容,不同对象的属性会保存在不同的堆内存中。
如果类中的某个属性希望定义为公共属性,也就是所有对象都可以使用的属性,则可以在属性前加上static关键字。
一:定义属性
class book{
private String title;
private double price;
static String pub="清华大学出版社";
public book(String title,double price) {
this.title=title;
this.price=price;
}
public String getInfo() {
return "图书名称:"+this.title+";图书价格:"+this.price+";出版社:"+this.pub;
}
}
public class STATIC {
public static void main(String[] args) {
// TODO Auto-generated method stub
book ba=new book("java开发",18.8);
book bb=new book("Oracle开发",12.2);
ba.pub="北京大学出版社";
System.out.println(ba.getInfo());
System.out.println(bb.getInfo());
}
}
分析:这里Book类中提供了一个static属性,也就是出版社是一个公共属性,存储在全局数据区。这样类中任何一个对象修改了内容后其他对象访问该内容也会受到影响。
static属性是公共属性,没有封装,所以可以通过所有对象的代表进行访问也就是类本身。
比如:book类的公共属性pub,可以在没有实例化对象前提下,直接通过book.pub访问,结果会影响其他对象。
class book{
private String title;
private double price;
static String pub="清华大学出版社";
public book(String title,double price) {
this.title=title;
this.price=price;
}
public String getInfo() {
return "图书名称:"+this.title+";图书价格:"+this.price+";出版社:"+this.pub;
}
}
public class STATIC {
public static void main(String[] args) {
// TODO Auto-generated method stub
book ba=new book("java开发",18.8);
System.out.println(ba.getInfo());
book.pub="北京大学出版社";//用公共类直接访问公共属性。
System.out.println(ba.getInfo());
}
}
二:定义方法
class book{
private String title;
private double price;
private static String pub="清华大学出版社";//封装公共属性pub,不可以book.pub,类内可以调用
public book(String title,double price) {
this.title=title;
this.price=price;
}
public String getInfo() {
return "图书名称:"+this.title+";图书价格:"+this.price+";出版社:"+this.pub;
}
public static void setPub(String p) {//定义一个设置属性值的公共方法
pub=p;
}
}
public class STATIC {
public static void main(String[] args) {
book.setPub("中国人大出版社");//同理调用公共方法
book ba=new book("java开发",18.8);
System.out.println(ba.getInfo());
}
}
注意:static方法只能调用static属性,不可以调用其他非static的属性或方法。哪怕是:this。
但是:非static可以调用任何属性和方法。
原因:所有的非static定义的结构,必须在类已经实例化对象后才会分配堆空间;而所有的static定义的结构,不受实例化对象控制,在没实例化对象时候都可以访问,因此不可以调用。
解释:为啥主方法是:public static 返回值类型 名称(参数),但是方法的调用:public 返回值类型 名称(参数),区别在于没有了:static。
public class STATIC { public static void main(String[] args) { fun();//static可调用static方法 } public static void fun() { System.out.println("Hello World!"); } }
public class STATIC { public static void main(String[] args) { new STATIC().fun();//static可调用static方法 } public void fun() { System.out.println("Hello World!"); } }
区别:只用实例化对象才能调用非static方法,即:new 类名()
如果一个类的对象没有属性只有方法,建议用static方法。
class MATH{
public static int add(int x,int y) {//用了static就可以不用实例化对象
return x+y;
}
}
public class STATIC {
public static void main(String[] args) {
System.out.println(MATH.add(10, 60));
}
}
利用static实现属性自动设置
class Book{
private String title;
private static int num=1;
public Book() {
this("标记为-"+num++);
}
public Book(String title) {
this.title=title;
}
public String getTitle() {
return this.title;
}
}
public class STATIC {
public static void main(String[] args) {
System.out.println(new Book("java").getTitle());
System.out.println(new Book().getTitle());
System.out.println(new Book().getTitle());
}
}
无参自动赋值,且对象之间不同。
this(参数):调用本类中直接赋值的构造函数
Java主方法的构成:
- public:主方法是程序的开始,必须可见操作。
- static:证明主方法是类名称调用的
- void:保证运行后不可以回头,一直运行下去。
- main:系统规定好的名称。
- String args[]:程序运行的参数