Results 1 to 2 of 2
  1. #1
    natdizzle's Avatar
    natdizzle is offline Nathan Nelson
    Join Date
    Jan 2009
    Posts
    100
    Rep Power
    0

    Default Classes in separate files

    Hi,

    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?

    Thank you,
    Nathan

  2. #2
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    2

    Default 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

Similar Threads

  1. Event Handling with Separate Classes
    By newbie123 in forum New To Java
    Replies: 9
    Last Post: 05-26-2011, 05:12 AM
  2. How to use functions in separate files
    By theodorekon in forum New To Java
    Replies: 2
    Last Post: 04-20-2010, 01:50 PM
  3. Should I separate my code into separate files?
    By Inks in forum New To Java
    Replies: 0
    Last Post: 03-26-2009, 12:12 AM
  4. LOG4j Logging in Separate Files for Threads
    By krishna_85 in forum New To Java
    Replies: 0
    Last Post: 03-05-2009, 04:23 PM
  5. Replies: 1
    Last Post: 11-18-2008, 04:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •