I currently have strings that are all the same throughout my classes, I was just wondering how I could make the value of the strings pass from the main class to the rest of the classes.

My reasons being that I am wanting to create a property file and have it set the strings to a default value which will access the main class.

Could someone briefly explain how I would go about this?

Recommended Answers

All 5 Replies

You can pass a reference to the main class into the constructors of the other classes, so that they can then call getter methods in the main class. Sharing the Strings themselves may be a bad idea - what happens when one of these values changes? Much better to use getters and never keep any local copies.

You can pass a reference to the main class into the constructors of the other classes, so that they can then call getter methods in the main class. Sharing the Strings themselves may be a bad idea - what happens when one of these values changes? Much better to use getters and never keep any local copies.

The strings are file locations, one being for a linux based system and the other for a windows based system so they shouldn't change. However, I agree it would be of much more use if I didn't limit them like that.

I've used getters before, but I'm not quite sure I follow you on how you would pull them from the main class. Could you elaborate? Or show a small example?

Main class defines getter for these Strings:
public String getDefaultFileLocation() { return defaultLocn; }

Main class instatiates an instance of a subclass, passing itself as a parameter:
SubClass sub = new SubClass(this);

SubClass keeps a copy in the constructor:
public SubClass(MainClass main) {
this.main = main);
}

SubClass methods can then query the Strngs from main:
File f = new File(main.getDefaultFileLocation());

commented: I appreciate your willingness to help bud, thank you +1

ps. I assume you've looked at System.getProperty(...) to get OS-dependent locations like user home directory etc?

Main class defines getter for these Strings:
public String getDefaultFileLocation() { return defaultLocn; }

Main class instatiates an instance of a subclass, passing itself as a parameter:
SubClass sub = new SubClass(this);

SubClass keeps a copy in the constructor:
public SubClass(MainClass main) {
this.main = main);
}

SubClass methods can then query the Strngs from main:
File f = new File(main.getDefaultFileLocation());

Thank you, I think I understand what I need to do. I'll give it a go when I get the chance and get back to you.


*Edit*
Yeah I have looked into System.getProperty, I just haven't applied it into my program quite yet. The following is one I had looked into awhile back when I was deciding how I would determine which OS the computer was using

String osName = System.getProperty("os.name");

I don't know a whole lot about property files at the moment either so I'm reading into that as well.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.