简体   繁体   English

为什么不调用Java中的静态方法?

[英]Why is call to static method in Java not being made?

I have a fairly standard creational pattern whereby a class exposes a static method for returning instances of itself, like so: 我有一个相当标准的创建模式,其中,一个类公开了用于返回自身实例的静态方法,如下所示:

public class MyClass {

    private MyClass(/*parameter list*/) {
        // internal construction
    }    

    public static MyClass GetMyClass(/*parameter list*/) {
        return new MyClass(/*parameter list*/);
    }
}

...

//this line wont break in the debugger and seemingly never gets called - why?
MyClass inst = MyClass.GetMyClass(/*parameter list*/);

However, inst is always null. 但是, inst始终为null。 I can't break on the line that calls the static method, the debugger just ignores it - what's going on? 我不能中断调用静态方法的行,调试器只会忽略它-发生了什么?

Edit: Thanks for the suggestions. 编辑:感谢您的建议。

  • All projects have been cleaned and rebuilt (manully in NetBeans) 所有项目均已清理和重建(在NetBeans中手动进行)
  • I have added a break in the static method and no it isn't hit. 我在静态方法中添加了一个中断,不,它没有被击中。
  • Yes, the code above is being called (ultimately) in a constructor for a Swing 'FrameView' though it surely shouldn't matter where I am calling this from, should it? 是的,上面的代码(最终)是在Swing'FrameView'的构造函数中调用的,尽管我从哪里调用它肯定不重要,应该吗?
  • There is no exception swallowing anywhere 吞食无处不在

Side note, other than the missing class declaration (which was a typo) why is this not valid Java? 旁注,除了缺少class声明(这是拼写错误)之外,为什么这不是有效的Java? Why is this obviously C# code? 为什么这显然是 C#代码? Explanations would be more helpful than downvotes :) 解释比起投票要有用:)

Edit II: The Params is just supposed to indicate a whole load of parameters - sorry if this confused anyone, I obviously know parameters have type declarations and so on, the code above was intended as a quick shorthand version rather than a full and (complicated) real sample... 编辑II: Params仅应指示参数的全部负载-抱歉,如果这使任何人感到困惑,我显然知道参数具有类型声明,依此类推,上面的代码旨在作为速记版本,而不是完整的(复杂的) )实际样本...

A couple of options: 有两个选择:

  • An exception is being thrown which you're somehow missing 抛出异常,您以某种方式丢失了该异常
  • You're not debugging the code that you think you are (ie your built code is out of date with your source code) 您不是在调试自己认为的代码(即,内置代码与源代码已过时)

The latter is the most likely one, IMO. 后者是最有可能的国际海事组织。

Apparently you're swallowing an exception inside the constructor something like: 显然,您正在吞下构造函数中的异常,例如:

try {
    // Something.
} catch (Exception e) {
}

You should never do that. 你绝对不应该那样做。 It makes debugging and nailing down the root cause much harder. 它使调试和确定根本原因变得更加困难。 Rather throw it or at least do a e.printStackTrace() . 宁可throw它,也至少要做e.printStackTrace() If throwing and you don't want to use the throws clause for some reasons, consider using a RuntimeException (or one of its subclasses). 如果抛出,并且由于某些原因不想使用throws子句,请考虑使用RuntimeException (或其子类之一)。 Eg 例如

try {
    // Something.
} catch (Exception e) {
    throw new RuntimeException("Construction failed.", e); // Try to be more specific, e.g. IllegalArgumentException or so. Or just write robust code, i.e. nullchecks and so on.
}

or (but in my opinion not very applicable in your case): 或(但我认为不适用于您的情况):

try {
    // Something.
} catch (Exception e) {
    e.printStackTrace();
}

I understand that you are trying to make a simple example to show your problem, however if you add the appropriate type statements into your sample code, then it both compiles and does what you expect. 我了解您正在尝试通过一个简单的示例来展示您的问题,但是,如果将适当的类型语句添加到示例代码中,则它既可以编译,又可以满足您的期望。

However, in your original codebase you could simply place the breakpoint in the static method to see whether or not it is called. 但是,在原始代码库中,您只需将断点放在静态方法中即可查看是否调用了该断点。

Maybe a simple question, but you never know… are you sure that you are running the code that you think you are running? 也许是一个简单的问题,但您永远不知道……您确定正在运行您认为正在运行的代码吗? That is, is everything recompiled and built from the latest sources? 也就是说,所有内容都是从最新资源重新编译和构建的吗?

There is nothing wrong with : 没错:

MyClass inst = MyClass.GetMyClass(Params);

It depends what is before or after that line of code. 这取决于该行代码之前或之后的内容。

Start by doing this: 首先开始:

public class MyClass 
{
    private MyClass(/*parameter list*/) 
    {
        System.out.println("entering MyClass(...)");
        // internal construction
        System.out.println("leaving MyClass(...)");
    }    

    // Java uses lower case for method names - so get not Get
    public static MyClass getMyClass(/*parameter list*/) 
    {
        final MyClass foo;

        System.out.println("entering getMyClass(...)");

        foo = new MyClass(...);

        System.out.println("leaving getMyClass(...)");

        return (foo);
    }
}

...

MyClass inst = MyClass.getMyClass(/*parameter list*/);

See if outside the debugger the code gets called. 查看是否在调试器之外调用代码。

If you are catching any exceptions, at the very least do: 如果您发现任何异常,请至少执行以下操作:

catch(final WhateverException ex)
{
    // at the very least do this so you can see that the exception happens
    ex.printStackTrace();
}

Avoid catching Throwable, Error, Exception, and RuntimeException. 避免捕获Throwable,Error,Exception和RuntimeException。 Infact the best way do do it is get rid of all the catch statements and then only add catches for what the compiler tells you that you have to have. 实际上,做到这一点的最佳方法是摆脱所有catch语句,然后仅针对编译器告诉您的必需条件添加catch。

The other thing is you do not say where MyClass inst = MyClass.getMyClass(/ parameter list /); 另一件事是您不说MyClass inst = MyClass.getMyClass(/ parameter list /); is called from. 被称为。 It is entirely possible that that line never gets hit. 这条线很可能永远不会被击中。

You mention that you're calling this from the constructor of a FrameView, but I assume you're talking about an implementation or extension of that interface/object. 您提到您是从FrameView的构造函数调用此函数的,但我假设您是在谈论该接口/对象的实现或扩展。 My reasoning was to make sure you wern't recursively invoking the constructor. 我的目的是确保您不会递归调用构造函数。

I think the reason why catching java.lang.Exception isn't catching the problem is because it is likely too specific in this case. 我认为捕获java.lang.Exception未能捕获问题的原因是因为在这种情况下它可能太具体了。 Try catching java.lang.Throwable which will catch errors like java.lang.NoClassDefFoundError - that frequently crops up when you have a jar missing somewhere. 尝试捕获java.lang.Throwable ,它将捕获诸如java.lang.NoClassDefFoundError之类的错误-当您在某个位置缺少jar时经常出现这种错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM