-1

I built a GUI class, and I get the pathname of the file I want to open in this GUI class. The pathname comes back as:

public String filePath = "C\blablah";

I want to actually open my file in another class, the "Network" class, so I wrote the following code to get the path string in the Network class:

String readFile = GUI.file();
Path file = Paths.get(readFile, "Network");

I tried a few different ways but it doesn't work, I thought this one would work but it comes back with

"Cannot make a static reference to the non-static method filePath() from the type GUI"

None of those classes are my main so I cannot instantiate a GUI in the "Network" class.

Please forgive if this is a newbie question.

Edit:

This is the method I wrote in the GUI class to access the filepath in other classes

public String file(){
    return filePath;
}
cachemoi
  • 343
  • 2
  • 13
  • Pass the `filePath` value to the other class via a method call or constructor, see [Passing Information to a Method or a Constructor](https://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html) – MadProgrammer Jan 22 '16 at 21:03
  • I tried and it gives back "Cannot make a static reference to the non-static method filePath() from the type GUI" – cachemoi Jan 22 '16 at 21:06
  • Did you [search](http://www.google.co.il/search?q=Cannot+make+a+static+reference+to+the+non-static+method) for the error? – user1803551 Jan 22 '16 at 21:06
  • I did, I do not want to instantiate a GUI object in the network class since it is not my main – cachemoi Jan 22 '16 at 21:08
  • You give no mention of the "*method filePath()*" in your code. There is only a variable named that. – user1803551 Jan 22 '16 at 21:14
  • I now added it to the question – cachemoi Jan 22 '16 at 21:17
  • @cachemoi No, you didn't, you seem to be trying to get a value from a class to which you don't have an instance of. I said pass the value from the class with the `filePath` to your class which wants to use it – MadProgrammer Jan 22 '16 at 21:59

1 Answers1

0

Ok, what you trying to do here is to access the static variable "filePath" from your GUI class, but this variable isn't static. You can change this by adding the keyword static in front of the variable name.

But I guess, that you in fact don't want to access a static variable from the class GUI, but the variable of a GUI object. In this case there's no way around but to make a GUI object with the specific path and access it from your network class.

Also note, that is a better practice to access class variables with a getter-method.

DavidR
  • 41
  • 8
  • `static` is never the solution for cross class communication and simply introduces more hazards making much more unpredicitable and dangrous – MadProgrammer Jan 22 '16 at 21:59
  • 1
    If there's only one possible file path, known at compile time, then static **might** be appropriate, but as @DavidR says, this is unlikely. Are there many instances of GUI, each with different paths? Or is there one "central" GUI object with a path obtained during its lifetime? If the latter, a singleton which you can access from anywhere might be helpful. – BryanT Jan 22 '16 at 22:25