简体   繁体   English

JAVA-将方法返回类型从void更改为String会立即创建“死”代码吗?

[英]JAVA - Changing method return type from void to String instantly creates 'dead' code?

I wrote a class a while back, which gets a specific set of values from specific XML files. 我不久前写了一个类,它从特定的XML文件中获取一组特定的值。 It can then print the results in Console and it works just fine. 然后可以在Console中打印结果,并且效果很好。 But of course I didn't make a class to print to console 但是我当然没有上课要打印到控制台

I need to get the values into a database. 我需要将值放入数据库。 There is a separate class that handles the db input. 有一个单独的类处理db输入。

But I am having great trouble getting the local variables from the method that loops the XML files returned/passed to the database class. 但是我很难从循环返回/传递给数据库类的XML文件的方法中获取局部变量。

I cannot get the values from inside the main method that reads them from the XML files (the values from the Nodelist 'nodes'). 我无法从从XML文件中读取值的main方法内部获取值(来自Nodelist'nodes'的值)。 I tried using a bunch of global variables, but they don't get updated. 我尝试使用一堆全局变量,但它们没有更新。 I read that using 'global variables' was the way to go, but I also read (and found out when coding), that JAVA makes a copy from the actual bits inside the object and works with that inside a method. 我读到使用“全局变量”是必经之路,但是我也读(并在编码时发现),JAVA从对象内部的实际位复制并在方法内部使用。 So when the method ends all the alterations made to variables are ignored and the value of the object is the same as it was before the method ran. 因此,当方法结束时,将忽略对变量所做的所有更改,并且对象的值与方法运行前的值相同。

Here is the code that gives the problem: 这是给出问题的代码:

import org.w3c.dom.*;
import javax.xml.xpath.*;
import javax.xml.parsers.*;
import java.io.IOException;
import org.xml.sax.SAXException;

public class xmlGrabber {
public static String NAME = "StartNaam"; // declare the global variable


  public String main(String[] args) 
   throws ParserConfigurationException, SAXException, 
          IOException, XPathExpressionException {

    DocumentBuilderFactory domFactory = 
    DocumentBuilderFactory.newInstance();
          domFactory.setNamespaceAware(true); 
    DocumentBuilder builder = domFactory.newDocumentBuilder();
    String docuInvoer = "C:/temp/document.xml";
    Document doc = builder.parse(docuInvoer);
    XPath xpath = XPathFactory.newInstance().newXPath();
       // XPath Query for showing all nodes value
    XPathExpression expr = xpath.compile("//@fieldvalue");

    Object result = expr.evaluate(doc, XPathConstants.NODESET);
    NodeList nodes = (NodeList) result;
    for (int i = 1; i < 24; i++) {
    // System.out.println(nodes.item(i).getNodeValue()); 
     //String bla = nodes.item(1).getNodeValue(); 
    String NAME = nodes.item(1).getNodeValue(); //Modify value of NAME

    } return NAME;
  }
//  public static String NAME(){
    //  System.out.println(NAME+"test");
    //  return NAME;        
    //  }
}

When trying to read the variable in a different class like so: 当尝试像这样在另一个类中读取变量时:

public class Debug {
 public static void main(String[] args) {
     xmlGrabber.main(null);
     String name = xmlGrabber.NAME;
     System.out.println(name); 
 }
}

The IDE throws the error: NAME cannot be resolved or is not a field IDE引发错误:NAME无法解析或不是字段

When the compiler checks for dead code, it makes an exception for a main method with the signature 当编译器检查无效代码时,它将使带有签名的main方法成为异常

public static void main(String[] args)

because the point of the main method is it is an entry point, something that you can call from the command line or from a script or something and kick things off. 因为main方法的要点是入口点,所以您可以从命令行或脚本中调用某些东西,然后将它们拉开。

When you change the return type then it doesn't match that signature, it's no longer a valid main method, just some method that nowhere else in the program calls: dead code. 当您更改返回类型时,它与该签名不匹配,它不再是有效的main方法,而只是程序中其他地方没有调用的某些方法:无效代码。

Also, when your main method stops running the program goes away. 同样,当您的主要方法停止运行时,程序也会消失。 For you to save the results of your xml parsing to a database either you should make calls from your xml parsing code to the data access class (alternatively you could write the results to stdout and pipe them to a separate java program that reads data from stdin and saves it to the database). 为了将xml解析的结果保存到数据库中,您应该从xml解析代码向数据访问类进行调用(或者,您可以将结果写入stdout并将其通过管道传输到单独的java程序中,该程序从stdin中读取数据并将其保存到数据库中)。

On this line you use 1 instead of i: 在这一行中,您使用1而不是i:

xmlGrabber.NAME = nodes.item(1).getNodeValue();

That probably explains the compiler warning and why printing to the console works but retrieving the nodes doesn't. 这可能解释了编译器警告,以及为什么打印到控制台有效但检索节点无效的原因。

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

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