Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

Newbie - custom class

Thread view: 
DustWolf - 21 Oct 2006 15:41 GMT
Hello,

I need a direct solution and no theory.

I would like to package some methods in a separate .class file. I'm not
sure how to:
1. Form the methods in my class file to make them available from the
outside
2. Convince another class to make the methods available
3. Compile the whole thing in a way that it actually works out

Help?
java@starflag.net - 21 Oct 2006 16:22 GMT
/* Put this in DustWolf.java */
public class DustWolf {
 public static void main(String args[]) {
   int n1 = 18;
   int n2 = 36;

   System.out.println("The sum of " + n1 + " and " + n2 + " is: " +
       Helper.add(n1, n2));
 }
}

/* Put this in Helper.java */
public class Helper {
   public static int add(int a, int b) {
     return a + b;
   }
}

/* Compile with:
  javac -classpath . Helper.java DustWolf.java

  Execute with:
  java -classpath . DustWolf
*/
> Hello,
>
[quoted text clipped - 8 lines]
>
> Help?
DustWolf - 21 Oct 2006 17:22 GMT
Works perfectly, thanks. :)
Lew - 22 Oct 2006 00:38 GMT
(Posting reorganized to eliminate top-posting)
>> Hello,
>>
[quoted text clipped - 8 lines]
>>
>> Help?

> /* Put this in DustWolf.java */
> public class DustWolf {
[quoted text clipped - 20 lines]
>    java -classpath . DustWolf
> */

Bear in mind that the simplified example appears to endorse putting classes in
the default package, which is a well-known Bad Idea.  It's may seem like a
complication if you're learning the basics of the "public" keyword, how to put
source into ".java" files and how to use javac, but it is just as important to
learn about packages as these other concepts.

To the OP: a "package" is related to what subdirectory the class file is in.
The simplified example put all the source code into the "default package",
which corresponded to the current directory "." mentioned in the "-classpath"
variables.  When you feel more comfortable with how to declare a method
"public", how to organize source into ".java" files and the other basics of
the Java(tm) language, you will need to understand about subdirectories and
packages.  The result will be something like:

 /* Put this in somewhere/DustWolf.java */
 package somewhere;
 public class DustWolf {
     ...
 }

 /* Put this in somewhere/Helper.java */
 package somewhere;
 public class Helper {
     ...
 }

 /* Compile with:
    javac somewhere/Helper.java somewhere/DustWolf.java

    Execute with:
    java -classpath . somewhere.DustWolf
 */
Régis Décamps - 22 Oct 2006 00:55 GMT
> (Posting reorganized to eliminate top-posting) > DustWolf wrote: >> Hello,
>  >>
[quoted text clipped - 10 lines]
> > Bear in mind that the simplified example appears to endorse putting classes in
> the default package, which is a well-known Bad Idea.

I agree.

As soon as you feel confident with the basics, I'd advise you to create
and manage your projects with Maven. Maven sugguests a directory layout
which is a compilation of best practices. And Maven is extremely simple
to use.

Signature

Régis



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.