public class Test { public void print(){ System.out.println("print"); } public static void m(Test t){ t.print(); } } 是可以的
t.print(); 已经是用对象在调用方法了,而不是静态调用了
这个问题值得讨论吗? public static void main(String args[]) { //你不要说你没在这里调用过非static方法. }而或许你可能是在某个地方看到这样的一句话:"在static方法中不能调用非static方法" 而这里所描述的是这样一种情况: public class Test { public static sMethod() { mMethod(); //这样调用是非法的,因为编译器不会为static方法传入this,因此不能把mMethod()编译成this.mMethod()了. } public void mMethod() { } }
很简单的啊,main方法中你说能不能调用非static方法啊!
public class Theclass { public void Theclass(){} public void showme() { System.out.println("showme!"); } }public static void main(String args) { new Theclass().showme(); }
return str.subString(1);
}staticMethod是静态方法 而subString是非静态的 不知道这样行不行啊...
public void print(){
System.out.println("print");
}
public static void m(Test t){
t.print();
}
}
是可以的
t.print(); 已经是用对象在调用方法了,而不是静态调用了
public static void main(String args[])
{
//你不要说你没在这里调用过非static方法.
}而或许你可能是在某个地方看到这样的一句话:"在static方法中不能调用非static方法"
而这里所描述的是这样一种情况:
public class Test
{
public static sMethod()
{
mMethod(); //这样调用是非法的,因为编译器不会为static方法传入this,因此不能把mMethod()编译成this.mMethod()了.
} public void mMethod()
{
}
}
public class Theclass
{
public void Theclass(){}
public void showme()
{
System.out.println("showme!");
}
}public static void main(String args)
{
new Theclass().showme();
}
如:MyClass.mystaticMethod();非static 方法是对象级别的方法,只有实例化了才可以调用.
如:MyClass obj = new MyClass(); obj.method1();下面的程序进一步说明一下:class MyClass {
static void mystaticMethod() {
method1(); // 错误,method1(0方法没有具体的对象
} void method1() {
}
void method2() {
method1(); // 正确,默认是this对象
}
}