How do you use another class file... Seriously

B

Bret Casanova

Can anyone tell me the exact process to use a class file in a project.
Every beginner website has the same crap about writing a "hello
world" program and then there's some presumed leap that you know how
to add class files.

Maybe this is one of those things that is considered so easy that
nobody ever mentions it but I've been looking for this process for
days and can't figure it out.

For example I have a class file that gets the path to the current
folder, the only method in it is a main method.

Now if I want to use that class file from some other project that
already has a main method how can I do it. What is the exact
"process" that I would use to somehow use that class file that gets me
the directory in a different project.

All I ever read is that class files are the building blocks of more
robust projects but I can't find anything anywhere that tells me how I
can actually use the damn class file. I'm just supposed to somehow
magically know this already.

If anyone could just assume that I am mentally retarded and tell me
exactly the steps I would need to do to use the class file I would
seriously appreciate it because I'm getting nowhere.

If it matters the ide I have is eclipse.


Thank you.
 
C

Craig

Hi Bret,

Bret said:
Can anyone tell me the exact process to use a class file in a project.
Every beginner website has the same crap about writing a "hello
world" program and then there's some presumed leap that you know how
to add class files.

Hmmm.. not really. Ever noticed the import statements? This is how you
use external classes in your app. You can import by specific class or
using the wildcard '*" to get all classes in a package. The methods of
those classes then become accessible in your code.
Maybe this is one of those things that is considered so easy that
nobody ever mentions it but I've been looking for this process for
days and can't figure it out.

Most of the good "basic" texts show all this very clearly.
For example I have a class file that gets the path to the current
folder, the only method in it is a main method.

This is a problem! You can only have one main method in your
application as thats the entry point to your program. You presumably
have access to the java file. You need to change the name to something
useful eg.
class = PathFinder
method = getCurrentFolder()
Now if I want to use that class file from some other project that
already has a main method how can I do it. What is the exact
"process" that I would use to somehow use that class file that gets me
the directory in a different project.

All I ever read is that class files are the building blocks of more
robust projects but I can't find anything anywhere that tells me how I
can actually use the damn class file. I'm just supposed to somehow
magically know this already.

Back to basics is all I can suggest. Forget you have an IDE and go back
to the commandline for a week. Get to grips with how to structure a
basic java progam. Take a look at Bruce Eckels "Thinking in Java" at
http://www.mindview.net Read the first two chapters until you understand
it completely! Try using the vast array of functionality thats
available in the java packages already.
If anyone could just assume that I am mentally retarded and tell me
exactly the steps I would need to do to use the class file I would
seriously appreciate it because I'm getting nowhere.

We all think we know more than we do and get frustrated when its
demonstrated so. Take it as an opportunity to grow your knowledge not
as a rebuff. I think your problem is you don't have a handle on the basics.

HTH

Craig
 
T

Thomas G. Marshall

Craig <[email protected]> coughed up the following:

....[rip]...
This is a problem! You can only have one main method in your
application as thats the entry point to your program.

Huh? This is not true----In many of my applications there are /large
numbers/ of main methods. Each of them forms a tiny little test harness for
a particular sub-section of the code.


....[rip]...
 
C

Craig

Thomas said:
Huh? This is not true----In many of my applications there are /large
numbers/ of main methods. Each of them forms a tiny little test harness for
a particular sub-section of the code.


Well if we are going to get picky:
<quote>
[The following rules apply to creating a main() method, which allows you
to run the class as an application. You can create any number of methods
called 'main' in a class (overloading), which take other arguments, are
not public and static, or return a value. To be able to run the class,
however, there must be one method called main that takes an array of
Strings as an argument, and that method must be static, and not return a
value. Otherwise you will get a runtime error when you try to execute
the class]

The main() method must be static, not return a value, and take an array
of strings.

Whether it is declared public is something of a controversy. The Java
Language Specification clearly states "The method main must be declared
public, static, and void. It must accept a single argument that is an
array of strings." On the other hand, 'The Complete Java2 Certification
Study Guide' by Roberts, Heller et al. states. "The main method is
declared public by convention. However, it is a requirement that it be
static...". Tests indicate that in Java 1.2 (and 1.3), you can execute a
program whose main() method is private, as bizarre as that may seem. You
cannot do this in Java 1.1. or in Java 1.4.1.
</quote>

Just because you can do it doesn't mean you should! IMO introducing
this type of thing into you code is unnecessary and for the OP your
comments muddy the waters somewhat.

Craig
 
A

Andrew Thompson

...
Indeed. In fact, it's already been answered there.

True Ryan, but note that the OP posted to c.l.j.help
*after* I had made that comment.

It might also have been handy for the OP to
mention the change on c.l.j.p. and refer to it
in the new post on c.l.j.help, but I feel the
OP displayed that they were capable of recognizing
and following sensible suggestions.

[ I'd have posted a response to c.l.j.help myself
if Knute, Alex and Tony had not chipped in quickly.
You provided some apt links BTW, I hope the OP is
still reading the thread. ]
 
S

Sudsy

Just because you can do it doesn't mean you should! IMO introducing
this type of thing into you code is unnecessary and for the OP your
comments muddy the waters somewhat.

I'm with Mr. Marshall on this one. Java enables independent class
development. When I'm working on a class, debugging it, etc., I
create a main method which serves as a test of the class methods.
I can test the class in isolation but don't have to remove the
main method when the class is incorporated into a larger project.
Hence I'll often end up with many main methods, one per class.
 
C

Craig

Sudsy said:
I'm with Mr. Marshall on this one. Java enables independent class
development. When I'm working on a class, debugging it, etc., I
create a main method which serves as a test of the class methods.
I can test the class in isolation but don't have to remove the
main method when the class is incorporated into a larger project.
Hence I'll often end up with many main methods, one per class.
Thats great for your needs but the OP was trying to do something pretty
basic and was possibly getting screwed up by having more than one main
method. If you are into independent unit testing using multiple main
method's is sensible. In the OP's case it was not.

Cheers
Craig
 
C

Craig

Sudsy said:
I'm with Mr. Marshall on this one. Java enables independent class
development. When I'm working on a class, debugging it, etc., I
create a main method which serves as a test of the class methods.
I can test the class in isolation but don't have to remove the
main method when the class is incorporated into a larger project.
Hence I'll often end up with many main methods, one per class.
BTW having every class "independent" is not a recipe for success.

Craig
 
T

Thomas G. Marshall

Craig said:
Well if we are going to get picky:

Not picky. You're just wrong.

<quote>
[The following rules apply to creating a main() method, which allows
you
to run the class as an application. You can create any number of
methods called 'main' in a class (overloading), which take other
arguments, are
not public and static, or return a value. To be able to run the class,
however, there must be one method called main that takes an array of
Strings as an argument, and that method must be static, and not
return a value. Otherwise you will get a runtime error when you try
to execute
the class]

That's right. The /CLASS/. But you said the following:

You:
This is a problem! You can only have one main
method in your *application* as thats the entry point
to your program.

Which is as wrong as can be.
 
T

Thomas G. Marshall

Craig <[email protected]> coughed up the following:

....[rip]...
BTW having every class "independent" is not a recipe for success.

Craig


Every /single/ one, usually not in a medium to large project. But keeping
as much as you can separate with their own test harnesses, well there is
/nothing/ wrong with that, and a /whole lot right/ with it.

Please look up the OO notion of "coupling", and why you should keep it to a
minimum. It is fundamental OO design.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top