java - 在 Java 中声明变量之前赋予另一个类变量值

标签 java variables

我正在尝试将值传递给可能尚未声明的变量。 从我的主要源类中,我为另一个类提供了一些值,但后来这些值似乎消失了。

源代码:

server.java(主要):

public class server {

public static void main(String[] args)  {

    //Print a simple message to the user to notify there is something going on...
    System.out.println("Starting server, please wait...");

    //Connecting all class files to the server.
    filehandler filehandlerclass = new filehandler();
    networking networkingclass = new networking();
    //End of class files connecting.

    //Preparing the filehandler's file information to open a new filestream.
    filehandlerclass.filetohandlename = "server";
    filehandlerclass.filetohandleextention = "ini";
    filehandlerclass.filetohandlepath = "configs\\";

    //Request a new filestream using the filehandler's file variables.
    filehandlerclass.openfilestream(filehandlerclass.filestream, filehandlerclass.filetohandle);

    //Checks if the filehandler has tried to open a filestream.
    if(filehandlerclass.filestreamopen == true) {
        //Request a check if the filestream was opened sucessfully.
        filehandlerclass.filestreamexists(filehandlerclass.filestream);
    }
    //If the filehandler has not tried to open a filestream...
    else    {
        System.out.println("Error: The filehandler does not seem to have tried to open a filoestream yet.");
        System.out.println("A possibility is that the server could not call the method from the filehandler properly.");
    }

    //Checks if the boolean "filestreamexists" from the filehandlerclass is true.
    if(filehandlerclass.filestreamexists(filehandlerclass.filestream) == true)  {
        //The filestream seems to exist, let's read the file and extract it's information.
        filehandlerclass.readfile(filehandlerclass.filestream);
    }
    else    {
        filehandlerclass.openfilestream(filehandlerclass.filestream, filehandlerclass.filetohandle);
    }
}
}

文件处理程序.java:

//Imports the java.io library so the filehandler can read and write to text files.
import java.io.*;

public class filehandler    {

//Variables for the filehandler class.
public String filetohandlename;
public String filetohandleextention;
public String filetohandlefullname = filetohandlename + "." + filetohandleextention;
public String filetohandlepath;
public String filetohandle = filetohandlepath + filetohandlefullname;

//Boolean that is true if the filehandler's "openfilestream"-method has tried to open a filestream.
//Is false as long as none filestreams have been touched.
public boolean filestreamopen = false;

//Declares a variable for the filestream to access text files.
public File filestream;
//End of variable list.

//Called to open a filestream so the server can load properties from text files.
public void openfilestream(File filestream, String filetohandle)    {

    //Tell the user that a filestream is about to be opened.
    System.out.println("Opening filestream for \"" + filetohandlefullname + "\"...");
    //Open a filestream called "filestream" using the variable "filetohandle"'s value
    //as information about wich file to open the filestream for.
    filestream = new File(filetohandle);
    //Turn the boolean "filestreamopen" to true so next time the server checks it's
    //value, it knows if the filehandler has tried to open a filestream.
    filestreamopen = true;
}

//Boolean that checks if the filestream exists.
public boolean filestreamexists(File filestream)    {
    //Tell the user that a check on the filestream is going on.
    System.out.println("Checking if filestream for \"" + filetohandlefullname + "\" exists...");
    //If the filestream exists...
    if(filestream.exists()) {
        //Tell the user that the filestream exists.
        System.out.println("Filestream for \"" + filetohandlefullname + "\" exists!");
        //Make the boolean's value positive.
        return true;
    }
    //If the filestream does not exist...
    else    {
        //Tell the user that the filestream does not exist.
        System.out.println("Filestream for \"" + filetohandlefullname + "\" does not exist!");
        //Make the boolean's value negative.
        return false;
    }
}

//Called to read files and collect it's information.
public void readfile(File filestream)   {
    //Checks if the file that is going to be read is a configuration file.
    if(filetohandleextention == "ini")  {
        //Tell the user that a configuration file is going to be read.
        System.out.println("Extracting information from the configuration file \"" + filetohandle + "\".");
    }
}
}

networking.java:

public class networking {

}

问题:

server.java 将为源文件提供命令并告诉它们要做什么。 除非 server.java 给了它们一个命令,否则源文件不会自行运行。 这样,我计划能够在 server.java 中编写简单的函数调用,以从不同的源文件中执行更大的任务。

server.java 似乎在声明变量之前传递变量“filetohandlename”、“filetohandleextention”和“filetohandlepath”,并且在声明变量时,将它们声明为“null”作为值。

结果:

Java compile/run error.

编译时没有出现任何错误。 我认为正在发生的只是与给出指定要读取的文件的正确值的变量不匹配。 它还抛出一个异常,我暂时没有兴趣去研究它,要么是因为“null.null”不存在,要么是我写错了代码。

最终请求:

有谁知道我是否可以制定一种接收变量值的方法 或者是否还有其他更合适的方法? 我可以在 server.java 中创建一个变量数组并从该数组中收集值吗?

非常感谢您抽出时间。

最佳答案

这个:

public String filetohandlename;
public String filetohandleextention;
public String filetohandlefullname = filetohandlename + "." + filetohandleextention;

将前两个变量初始化为 null,将第三个变量初始化为“null.null”。请注意,如果您更改组成 filetohandlefullname 的组件变量之一,它不会更改 filetohandlefullname 的值。如果您希望发生这种情况,则应将 filetohandlefullname 替换为执行附加操作的方法。

这个:

public void openfilestream(File filestream, String filetohandle)  

将不同的变量filetohandle传递到该方法中。该变量与 this.filetohandle 不同。

我认为这段代码(上面)存在很多问题,我会执行以下操作。

  1. 将通过其他变量实例化的变量替换为动态执行此操作的方法。这样,当您更改 var1 并希望更改 var2 的值时,这将通过方法返回自动发生。例如创建一个私有(private)方法 getFileToHandleFullName() 并存储相应的变量
  2. 使用 this 作用域类成员
  3. 在可能的情况下,将这些成员设置为最终,这样您就不会无意间更改它们

关于java - 在 Java 中声明变量之前赋予另一个类变量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13261302/

相关文章:

java - CF推送在Cloud Foundry中部署Spring Boot应用失败

Matlab:将 'who' 输出作为参数传递

JavaScript - 使用变量创建嵌套值

Python 自由变量。为什么会失败?

java - 如何在安卓日历中添加邮件收件人?

java - 如何获取引发自定义异常的类的名称

java - java android 每 5 秒扫描一次可用的无线网络

PHP 存储 session : Can't Seem to Serialize Session Variables

c# - 发送对变量的引用而不是它的值

java - LinearLayout 中的模糊背景图像