![]() |
|
[Java] How do I call a variable from one class into another class? - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [Java] How do I call a variable from one class into another class? (/showthread.php?tid=5022146) |
How do I call a variable from one class into another class? - Paathurnax - Oct 14, 2015 I've got two classes set up, and I need to call three variables from one class into another class, but I'm not sure how to do it. Here's the two classes. Code public class BorrowerCode public class AddressWhat I need to do is call the variables postcode, town and street from the Address class into the Borrower class using one method. I'm trying to use Address address = new Address(); but that's not working and I'm not sure why. I also need to initialise an attribute called Address in the Borrower class using the information given to the constructor in the Address class. Would really appreciate some help here. * - matt - Oct 14, 2015 Make a new instance of the class Code Borrower borrower = new Borrower();* - Ramsey - Oct 14, 2015 Use public static String myStr = "Hackforums"; Then call it using the class name then the String name (ie. myStr) from the other class. I hope this is what you're asking for. * - Paathurnax - Oct 14, 2015 (Oct 14, 2015 01:33 PM)Midnight Blue™ Wrote: Make a new instance of the class Where exactly do I put this? I tried adding it into the constructor but it threw up an error. * - Yuikoro - Oct 14, 2015 (Oct 14, 2015 01:39 PM)Vancouver Wrote: Where exactly do I put this? I tried adding it into the constructor but it threw up an error. If you have your variables private, you can't publicly access them through just using the object. You'll have to use a method which returns the variable. Ex: Borrower borrow = new Borrower(); borrow.getSomeBorrowVariable(); * - Paathurnax - Oct 14, 2015 (Oct 14, 2015 01:43 PM)Sender Wrote: If you have your variables private, you can't publicly access them through just using the object. You'll have to use a method which returns the variable. Why would I be creating a new instance of the Borrower class if I want to pull the information from the Address class into the Borrower class? * - chrisman0091 - Oct 14, 2015 Don't take anything I say too seriously as I'm not a great programmer and there might be a very good reason not to, but why not just make the variables public instead of private and then call them with className.variable? * - Yuikoro - Oct 14, 2015 (Oct 14, 2015 06:14 PM)chrisman0091 Wrote: Don't take anything I say too seriously as I'm not a great programmer and there might be a very good reason not to, but why not just make the variables public instead of private and then call them with className.variable? You would need to make the variables static as well if you wish to statically access a variable. Making them public allows you to access them after an object of that class type has been made. * - chrisman0091 - Oct 14, 2015 (Oct 14, 2015 06:17 PM)Sender Wrote: You would need to make the variables static as well if you wish to statically access a variable. Making them public allows you to access them after an object of that class type has been made. Yeah, that's what I meant. Public & static, I believe somebody else also said that above or showed code with it like that. * - mienkrsftajz - Oct 14, 2015 (Oct 14, 2015 01:35 PM)xRamsey Wrote: Use No, not even close. The attributes are bound to the instance - making it static means it's bound to the class, and therefore shared by all instances. @OP: you have to add the address to the Borrower class as another member attribute (I'm assuming each borrower will have their own address). So, do it the same way you handle name and whatnot; then you can pass it out through an accessor, or handle it internally within the object. Eg: PHP Code |