|
|
Newbie - custom class
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?
|
|
0
|
|
|
|
Reply
|
jure.sah (7)
|
10/21/2006 2:41:59 PM |
|
/* 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
*/
DustWolf wrote:
> 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?
|
|
0
|
|
|
|
Reply
|
java1022 (4)
|
10/21/2006 3:22:12 PM
|
|
Works perfectly, thanks. :)
|
|
0
|
|
|
|
Reply
|
jure.sah (7)
|
10/21/2006 4:22:04 PM
|
|
(Posting reorganized to eliminate top-posting)
> DustWolf wrote:
>> 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 wrote:
> /* 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
> */
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
*/
|
|
0
|
|
|
|
Reply
|
lew35 (439)
|
10/21/2006 11:38:36 PM
|
|
On Oct 22, 1:38 am, Lew <l...@nowhere.com> wrote:
> (Posting reorganized to eliminate top-posting) > DustWolf wrote: >> Hello,
> >>
> >> I need a direct solution and no theory.
> >>
> >> I would like to package some methods in a separate .class file. I'm n=
ot
> >> 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?
> > Bear in mind that the simplified example appears to endorse putting cla=
sses 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.
--=20
R=E9gis
|
|
0
|
|
|
|
Reply
|
regis.decamps (4)
|
10/21/2006 11:55:23 PM
|
|
|
4 Replies
19 Views
(page loaded in 0.111 seconds)
Similiar Articles: Change JFrame icon via custom LaF - comp.lang.java.gui... Is there a way to change the JFrame icon via a custom ... I'm a newbie so take my advise with a grain of salt. ... With the JInternalFrame class you can display a JFrame ... Open new JPanel (JFrame, ...) by clicking JButton - comp.lang.java ...I am a newbie in Java and I've been searching for a whole ... Next a JButton to click to set the number in ... class ... Hi All, Im very new to ... fill ... to add a custom JPanel ... comp.lang.java.gui - page 95... 11/7/2003 11:50:39 PM) I'd like to create a custom ... I have created a class that extends Panel and included ... sdk 1.4.2 comments or fixes for a newbie...? J ... Background not repainted after display of tooltip. - comp.lang ...My subclass of JPanel has a custom paintComponent ... Graphics; import java.awt.MediaTracker; public class ... Newbie attempt: simple animation *insanely slow* - comp ... Generating a MAC using a pre-shared key - comp.lang.java.security ...In my searching, I have found plenty of utility classes ... still have an itch to know why Mike Amling's custom ... Newbie Grief with the Lending Library Sample - comp ... Create an HTML report from this dataset - comp.soft-sys.sas ...... report from a dataset that has anywhere from 1-5 class ... Newbie with SAS - comp.soft-sys.sas I'm working in a IBM ... Using ASP.NET to Create Multi-Page Custom Reports The ... Change video resolution in solaris 10 - comp.unix.solaris ...Maybe I should have a class about Solaris...! ... of: /usr/dt/config/Xservers on how to set up a custom ... Well, I'm a really "newbie" in solaris :-). Therefore I ... Packet network simulation, routing table. - comp.soft-sys.matlab ...... router-2621 ... shows how to use the Custom Packet ... Newbie: cannot connect the WAN / Internet - comp.unix ... routing table is available in simulation log under class ... trouble Inserting OLE objects into drawings - comp.cad.solidworks ...Best Regards, -- Bob Hanson CEO Custom Programming Unlimited LLC SolidWorks ... The particular file type that I am having trouble with is .jpg, however I ... classes ... input & output in assembly - comp.lang.asm.x86I'm a complete newbie so plz excuse Theoretical bit ... on your hard drive: http://cs.nyu.edu/~yap/classes ... code org 100h ; Call custom ... Newbie Custom Class QuestionHello, I have what is probably a really simple question. I have a class that I created, similar to this: Public Class GridBuilder Private _moveablecolumns ... Instantiating Custom Class files in ASP.NET Code-behind files ...Instantiating Custom Class files in ASP.NET Code-behind files Newbie Question. Jul 16, 2009 09:37 PM | LINK 7/12/2012 4:30:39 PM
|
|
|
|
|
|
|
|
|