在Java中声明变量之前给出另一个类变量值

时间:2012-11-06 23:56:10

标签: java variables

我正在尝试将值传递给可能尚未声明的变量。 从我的主要源类我给另一个类一些值,但后来看起来价值已经消失了。

源代码:

server.java(main):

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);
    }
}
}

filehandler.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中创建一个变量数组并从该数组中收集值吗?

非常感谢您的时间。

2 个答案:

答案 0 :(得分:3)

此:

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的值,这将通过方法返回自动发生。例如,创建一个私有方法getFileToHandleFullName()并将相应的变量加入
  2. 范围类成员this
  3. 尽可能让这些成员final,以免无意中更改

答案 1 :(得分:0)

这一行

public String filetohandlefullname = filetohandlename + "." + filetohandleextention;
实例化类时执行

(即filehandler filehandlerclass = new filehandler();)。在那个时间点,两个变量都未设置,因此filetohandlefullname被初始化为null.null

但是您的代码还存在许多其他问题,例如

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

例如,您传递的参数是来自同一实例的字段。这种方法完全无用,因为该方法已经可以访问这些方法,而且非常令人困惑。

也许有点争议:

//Make the boolean's value positive.
return true;

评论只有在他们澄清代码时才会有用,如果没有它们就不那么明显,而且它们必须是100%真实的,而不是经常发生的,程序希望代码能做什么。在这个特定情况下,这些条件都没有实现,因为注释没有说明发生了什么,实际上方法的返回值是设置的,而不是一些非描述性的“布尔值”