Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

twoodcc

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
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?
 

myawn

macrumors member
Nov 9, 2006
52
3
Marietta, GA
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

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
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

macrumors 6502a
Mar 8, 2007
944
2
Cambridge, MA
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

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
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

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
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

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
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

macrumors 6502
Apr 9, 2006
435
16
Kirkland, WA
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
 

twoodcc

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
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

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

macrumors 603
Jul 23, 2002
5,479
1
Holly Springs, NC
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

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.