Hi everyone,

I'm currently trying to do a bit of java programming after not looking near it for almost a year. Most of what I've been doing has been C/C++, and I was just wondering -

in C++ you could have a header file with functions only in it, say "MyFunctions.h"

int functionA ()
{
    return 10;
}

string functionB ()
{
    return "hello";
}

double functionC ()
{
    return 5.5;
}

And include this file in another - so you could just call "double d = functionC ();", so i was wondering is there a way to do this in Java, or would I have to put all the functions within a class, create an instance of the class and call them that way? A bit of a stupid question I know but any help would be much appreciated :)

Edited 4 Years Ago by SCass2010

From the code above, do you mean a constant? You can create a static class where you can just call MyClass.functionc(). You do not need to create a new instance of the class. If that is what you are looking for.

ps
If you import static the class then you can use its static methods without prefixing the class name
ie

import static java.lang.Math.*;
...
x = cos(y);  // static method cos is resolved via the import static for Math

Ah right, forgot about static methods in java... it really has been a while :) thanks again!!!!

This question has already been answered. Start a new discussion instead.