PDA

View Full Version : java question




twoodcc
Feb 7, 2008, 11:21 AM
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?



toddburch
Feb 7, 2008, 11:27 AM
New them up!

twoodcc
Feb 7, 2008, 11:38 AM
New them up!

what do you mean? i created the new objects, but in the other class outside of the main method

myawn
Feb 7, 2008, 11:49 AM
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?

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.

twoodcc
Feb 7, 2008, 11:58 AM
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.

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

yeroen
Feb 7, 2008, 12:02 PM
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?

twoodcc
Feb 7, 2008, 12:15 PM
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?

well, i'm only doing it b/c i was told to. but yeah, it doesn't make much sense to me either.

twoodcc
Feb 7, 2008, 12:20 PM
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?

twoodcc
Feb 7, 2008, 06:00 PM
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

Mernak
Feb 7, 2008, 07:55 PM
you should just be able to use new like someone had suggested, the syntax is 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

twoodcc
Feb 7, 2008, 08:18 PM
you should just be able to use new like someone had suggested, the syntax is 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

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

jeremy.king
Feb 7, 2008, 09:36 PM
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

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

twoodcc
Feb 8, 2008, 01:38 AM
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

thanks. it doesn't matter now, i've already turned the assignment in.