Results 1 to 2 of 2
Thread: Classes in separate files
- 07-25-2012, 05:17 AM #1
Classes in separate files
I have one class that contains a main method, then another that does not. I want to be able create objects of the non main method class and be able to use them in any part of the main class. I'm also need to be able to get variables from the main class and use them in the class without the main method. Some people have suggested extending one class and importing one class, but when I do that the class can't find the file, should I move the class from my workspaces main file?
- 07-25-2012, 05:35 AM #2Student
- Join Date
- Jul 2012
- United States
- Rep Power
Re: Classes in separate files
If your 2 classes are in the same package, then you can use the public and default members as well as the constructor freely between the 2 classes.
If your 2 classes are in different packages, then you will need to import Class A into Class B in order for Class B to use Class A's public members. However, it sounds like you want Class A and Class B to be co-dependent on each other, and this does not sound like a good idea.
For more info on access-control, see here: Controlling Access to Members of a Class (The Java™ Tutorials > Learning the Java Language > Classes and Objects)"Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill
- By newbie123 in forum New To JavaReplies: 9Last Post: 05-26-2011, 05:12 AM
- By theodorekon in forum New To JavaReplies: 2Last Post: 04-20-2010, 01:50 PM
- By Inks in forum New To JavaReplies: 0Last Post: 03-26-2009, 01:12 AM
- By krishna_85 in forum New To JavaReplies: 0Last Post: 03-05-2009, 05:23 PM
- By Fliz in forum New To JavaReplies: 1Last Post: 11-18-2008, 05:48 PM