For Programmers: Free Programming Magazines  


Home > Archive > Java Help > June 2007 > Initiating a class









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Initiating a class
christopher_board@yahoo.co.uk

2007-06-28, 10:09 pm

hi all.

I am trying to write a program that has a separate class file to the
rest of the functions within the program. How would I go about making
a section of the program run what it is inside the class.

Any help in this matter would be appreciated

Thank you

kaldrenon

2007-06-28, 10:09 pm

On Jun 28, 11:29 am, christopher_bo...@yahoo.co.uk wrote:
> hi all.
>
> I am trying to write a program that has a separate class file to the
> rest of the functions within the program. How would I go about making
> a section of the program run what it is inside the class.


This vague description isn't very helpful. Are you saying that you
have a class (Foo, for example) with a main method and that you want
to use a separate class (Bar) from within Foo's main()? Is Bar
static?

If that's the case and Bar is static, it's as simple as calling
Bar.method(); from within Foo. you just have to make sure Bar.class is
in the same directory as Foo.class when you run it.

Please give some more detail on the situation.

Mark Space

2007-06-29, 8:09 am

christopher_board@yahoo.co.uk wrote:
> hi all.
>
> I am trying to write a program that has a separate class file to the
> rest of the functions within the program. How would I go about making
> a section of the program run what it is inside the class.
>
> Any help in this matter would be appreciated


As Kal said we really don't have enough information. It would be
helpful for you to explain more about what is going on. This is either
really easy to do, or really really hard. :-)

For really easy, you can just import a class. If you do this:

import java.util.Date;
class Main {
static public void main( String [] args ) {
Date d = new Date();
System.out.println( d ); // What's the default date?
}
}

You've just imported a separate class file, Date.class (compiled from
Date.java) into your program and run it's toString() method. (Note: I
didn't check to make sure the above compiles.)

You can do the same thing on your own. Place two public classes in
separate .java files in the same directory (don't use a package
statement inside either class). Compile and run from that same
directory. Have one invoke the other (no import needed). It should
work fine since both will use default package space.

It works the same if you do use a package command, and you place the
..java files in the appropriate part of your CLASSPATH variable. Just
name the files fully ( "java.util.Date d = new java.util.Date();" ) or
import them and it'll all just work.

Now for wholly separate .jar files, or libraries that aren't on your
classpath but need to be downloaded automagically from a website, or
running wholly separate programs, it's different. We'll need much more
info about exactly what you're trying to do.
Roedy Green

2007-06-29, 8:09 am

On Thu, 28 Jun 2007 08:29:33 -0700, christopher_board@yahoo.co.uk
wrote, quoted or indirectly quoted someone who said :

>I am trying to write a program that has a separate class file to the
>rest of the functions within the program. How would I go about making
>a section of the program run what it is inside the class.
>
>Any help in this matter would be appreciated


If you want to run a static method x inside class X you would write
X.x()

If you want to run an instance method inside class X, you first must
instantiate an object of type X with the constructor.

X myobject = new X();

Then you can invoke the method on it:

myobject.x();

see http://mindprod.com/jgloss/constructor.html
http://mindprod.com/jgloss/static.html
http://mindprod.com/jgloss/instance.html
http://mindprod.com/jgloss/method.html
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com