//:Store.java
class Store
{
public static void main(String[] args)
{
System.out.println(storage("Hi,miao!"));
}
static int storage(String s)
{
return s.length() * 2 ;
}
}
/*
Output:16
*///:~
若storage前無static無法通過編譯,“無法從靜態上下文中引用非靜態方法”
可以將storage放在另一個類中:
//:Store.java
class Store
{
public static void main(String[] args)
{
Another t = new Another();
System.out.println(t.storage("Hi,miao!"));
}
}
class Another
{
int storage(String s)
{
return s.length() * 2 ;
}
}
/*
Output:16
*///:~
這樣就可以了!