public class TestExample { public static void main(String[] args) { new TestExample().print(); }
private void print() { System.out.println("HelloWorld"); }
} 这样在print方法时没有static所以就应该使用实例化对象来调用非static方法, 所有的非static方法几乎都有一个特点:方法要由实例化对象调用
class Book{ private static int count=0; private String title; public String getTitle() { return this.title; } public Book(){ this("NOTITLE-"+count++); } public Book(String title) {
this.title=title; } } public class TestExample{ public static void main(String[] args) { System.out.println(new Book().getTitle()); System.out.println(new Book("开发实战经典").getTitle()); System.out.println(new Book().getTitle()); } }