Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中声明变量之前,给另一个类variablevalues_Java_Variables - Fatal编程技术网

在Java中声明变量之前,给另一个类variablevalues

在Java中声明变量之前,给另一个类variablevalues,java,variables,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("Starti

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

源代码: 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);
    }
}
}
//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 + "\".");
    }
}
}
public class networking {

}
filehandler.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);
    }
}
}
//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 + "\".");
    }
}
}
public class networking {

}
networking.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);
    }
}
}
//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 + "\".");
    }
}
}
public class networking {

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

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

结果:

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

最终请求: 有人知道我是否可以制作一个接收变量值的方法吗 或者是否有其他更合适的方法? 我可以在server.java中创建一个变量数组并从该数组中收集值吗

非常感谢您抽出时间。

这:

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

这:

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

我认为这段代码(上面)有很多问题,我会做以下工作

  • 用动态执行此操作的方法替换通过其他变量实例化的变量。这样,当您更改
    var1
    时,您希望更改
    var2
    的值,这将通过方法返回自动发生。e、 g创建一个私有方法
    getFileToHandleFullName()
    并保存相应的变量
  • 使用
  • 在可能的情况下,将这些成员设置为final,这样您就不会无意中更改它们
  • 这:

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

    这:

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

    我认为这段代码(上面)有很多问题,我会做以下工作

  • 用动态执行此操作的方法替换通过其他变量实例化的变量。这样,当您更改
    var1
    时,您希望更改
    var2
    的值,这将通过方法返回自动发生。e、 g创建一个私有方法
    getFileToHandleFullName()
    并保存相应的变量
  • 使用
  • 在可能的情况下,将这些成员设置为final,这样您就不会无意中更改它们
  • 这条线

    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%真实,而不是像经常发生的那样,程序希望代码做什么。在这种特定情况下,这两个条件都不满足,因为注释没有说明发生了什么,实际上方法的返回值是设置的,而不是这一行中一些不起眼的“布尔值”

    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%真实,而不是像经常发生的那样,程序希望代码做什么。在这种特定情况下,这两个条件都没有满足,因为注释没有说明发生了什么,实际上方法的返回值是设置的,而不是一些不起眼的“布尔值”

    您应该真正学会在Java代码中使用camel case。例如,filestreampen。此外,Java类还包括