静态(static)方法没有this指针,我觉得只能hard code
解决方案 »
- java命令行编译问题?
- 求一正则表达式
- 我要建一个tomcat项目,总是划红叉,大概是tomcat那还不对,咋办?
- 请教一个关于Swing刷新的问题
- 急救,SOCKET通讯时DataInputStream.readInt()时偶尔程序死
- 进者有分了!关于一个jtree的问题.
- 如何使用WebLogic的LDAP服务,最好有相关文档和教材,拜谢
- 如何在java中使用自己定义的字符!
- MD5加密函数在netscape中运行的时候抛出NullPointer异常,但是在IE中运行是正常的,请大虾提示!!!(代码内详)
- 情况来了,本人使用LOG4J记录日志,输出的日志内容时而在控制台显示时而记录到日志文件中
- [求助] JDK1.4方法读文件出现乱码,请帮忙!
- 如何复制对象?
代码如下
class B
{
public static void TT()
{
try{
throw new Exception();
}
catch(Exception e){
StackTraceElement[] elems=e.getStackTrace();
int deeps=elems.length;
if(deeps<2) {
System.out.println("illegal!");
}
else{
System.out.println("this static method of class "+
elems[0].getClassName()+" is invoked by class "+
elems[1].getClassName());
}
}
}
}
class A{
public static void main(String args[])
{
B.TT();
}
}
你看看结果!!
但好象编译不过,StackTraceElement是哪个包里的类,可否解释一下?
Exception in thread "main" java.lang.NoSuchMethodError
at B.TT(A.java:9)
at A.main(A.java:25)我在api理查了一下确有此类,由此方法,可为什么会有这样的错误呢?
可否解释一下
你的意思是不是这样?
仅仅是通过静态方法得到该类的名字?class Test
{
public static String getName ()
{
return Test.class.getName();
}
public static void main (String[] args)
{
System.out.println("The name of class is: " + Test.getName());
}
}
有了马上结贴
{
......
}///////////////////////////////////////////////////
//not static method.
{
...
methodNeedCallerClass(this.getClass());
...
}
//static method in Class Caller.
{
...
methodNeedCallerClass(Caller.class);
...
}