executing functions from a package file

I have a package setup, and one of the files in the package is commands.java. I've compiled this file successfully. Inside of it I have function that returns a value. Is there a way to call this fuction from a different file that includes this package? I've tried and I can't seem to get it to work.

[309 byte] By [dazmana] at [2007-10-3 0:39:40]
# 1
Yes, there is a way, although no file ever "includes" a package in Java.Not having seen what you did and not having an explanation of what "doesn't work" means, I can't offer any advice other than to tell you to google for something like java package tutorial.
jverda at 2007-7-14 17:33:45 > top of Java-index,Java Essentials,Java Programming...
# 2
I have an example of a package but it doesnt execute a function within an included file, it does something like :new DataFile datafile is a class in the package.
dazmana at 2007-7-14 17:33:45 > top of Java-index,Java Essentials,Java Programming...
# 3

This doesn't appear to have anything to do with packages whatsoever but more to do with when you call static vs nonstatic methods.

If a method is static you can call it directly on the class.

If a method is non static you must call it on an instance.

Importing a package does not mean that you can call methods randomly without any context. So no matter if the method is static or not you still have to invoke the method on a class or object as appropriate.

cotton.ma at 2007-7-14 17:33:45 > top of Java-index,Java Essentials,Java Programming...
# 4
so then should I do : new commands(var);Would that work?How could I invoke it?
dazmana at 2007-7-14 17:33:45 > top of Java-index,Java Essentials,Java Programming...
# 5
In short as appropriate. For your particular case I have no idea becauseyou haven't posted any code.Please consider posting formatted code for tailored help.
cotton.ma at 2007-7-14 17:33:46 > top of Java-index,Java Essentials,Java Programming...
# 6

main file..

import java.io.*;

import includes.*;

class test

{

public static void main(String args[])

{

dothis();

}

}

commands.java

package includes;

import java.io.*;

public class commands

{

public void dothis()

{

System.out.println("worked?");

}

}

Message was edited by:

dazman

dazmana at 2007-7-14 17:33:46 > top of Java-index,Java Essentials,Java Programming...
# 7

Revised main class

import java.io.*;

import includes.*;

class test

{

public static void main(String args[]);

{

commands commandsObject = new commands();

commandsObject.dothis();

}

}

You really might want to go through the online tutorials available on this site starting with http://java.sun.com/docs/books/tutorial/java/concepts/index.html

And it's good to follow some basic conventions as well. Class names should start with CAPITAL letters. ie. Commands and Test

cotton.ma at 2007-7-14 17:33:46 > top of Java-index,Java Essentials,Java Programming...
# 8
Do they have to start with capitals? Because my commands.java file compiled succesfully.
dazmana at 2007-7-14 17:33:46 > top of Java-index,Java Essentials,Java Programming...
# 9
> Do they have to start with capitals?No. But is conventional that they do so. Following conventions makes iteasier for other to read and understand your code.
cotton.ma at 2007-7-14 17:33:46 > top of Java-index,Java Essentials,Java Programming...
# 10
Ok thanks for the advice. I tried with the new code and it worked successfully. Thanks a lot. :)
dazmana at 2007-7-14 17:33:46 > top of Java-index,Java Essentials,Java Programming...