java question

Discussion in 'Mac Programming' started by twoodcc, Feb 7, 2008.

  1. macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #1
    ok, so i have created my own object in java. i have 5 different classes. 1 class is an abstract class, and it also has the main method in it. i created the objects in a different class. how can i access those objects from the main method?
     
  2. macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #2
    New them up!
     
  3. thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #3
    what do you mean? i created the new objects, but in the other class outside of the main method
     
  4. macrumors member

    myawn

    Joined:
    Nov 9, 2006
    Location:
    Marietta, GA
    #4
    Figuring out how Java classes get references to each other is frequently a tough issue to grapple with.

    One possibility, as another poster indicated, is to create them within your main method (via new) so that you'll have the references to them. Other possibilities are to have methods on those other classes which return references (factory methods) or to use some sort of 'locator' class which knows how to find all the various classes (and thus objects would need to register themselves when created).

    Not sure about having the main method in an abstract class -- there might be a valid reason to do that, but offhand it seems an odd design.
     
  5. thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #5
    thanks for the reply. so i would create them both in the other class and in the main method?

    btw, this is an assignment, but all the examples in the book say to create the objects in the main method.

    and you're right, it doesn't make sense to me to put the main method in the abstract class. right now, i have the main method in the class where i make the objects, and the program works. but the teacher wants it differently. i might just have to turn it in like this, if i can't figure this out
     
  6. macrumors 6502a

    yeroen

    Joined:
    Mar 8, 2007
    Location:
    Cambridge, MA
    #6
    Why are you putting main in the (abstract) base class? Even though I come from C/C++, from what I know of Java that sounds like a contorted, if not unworkable, design (someone correct me if I'm wrong).

    Shouldn't main() be in it's own class that imports the declarations (or whatever you call them in Java) of your class heirarchy? A main class or a "driver class" or something?
     
  7. thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #7
    well, i'm only doing it b/c i was told to. but yeah, it doesn't make much sense to me either.
     
  8. thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #8
    the teacher emailed me back and said that you can have objects in any class, and that you can have objects in the main class that calls objects in the other class.

    so how do i create an object that calls another object?
     
  9. thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #9
    well i think i got it. probably not what the teacher wants, but it works at least.

    i had to make the objects static, and then call classname.objectname.method(); for it to work.

    let me know if there's a better way
     
  10. macrumors 6502

    Joined:
    Apr 9, 2006
    Location:
    Boston, MA
    #10
    you should just be able to use new like someone had suggested, the syntax is
    Code:
    ClassName instanceName = new ClassName();
    instanceName.method();
    It should work if the class file is in the same folder as the java file that you are writing.

    Don't know how much easier this is than you way, but with this you don't have to type in the path each time.

    P.S. This way also allows non-static methods
     
  11. thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #11
    thanks for the reply.

    see, i already did this in the other class file:

    ClassName instanceName = new ClassName();

    the problem was calling it. i could call it if i made the object static, and put the classname in front of the instanceName
     
  12. macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #12
    Probably best if you posted full source code for us to take a looky. Keep in mind you can call methods of another class if its

    a) declared as public
    b) declared as protected, and the calling class is a subclass or in the same package
    c) default (aka package private) - belongs to the same package.

    I don't know that you have covered these yet in your class, but read all about access modifiers at http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html
     
  13. thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #13
    thanks. it doesn't matter now, i've already turned the assignment in.
     

Share This Page