-2

I'm begining with java and I would like to know, if there's a possibility to declare a method (function) in class, but its body to place in another file because of better code arrangement.

//Name.h

class Name
{
   void Function();
};


//Function.h"

void Name::Function() //this is what I'm trying to find
{
   //the functions body
}


//Main.cpp

int main()
{
   Name name;
   name.Function();
}

Is there such a possibility in java? I know there are no header files, but is there another option? Thanks for Your answers!

I tried to search on internet but didn't find anything

  • 4
    There is nothing like this in Java. – markspace Feb 25 '23 at 19:06
  • 2
    Why would you want to do this? It's hard to understand how this would make for better code structure. – Unmitigated Feb 25 '23 at 19:08
  • 5
    No. I'm curious why you would want this? The need for headers and forward declarations in C++ is usually considered to be _annoyance_. In fact, recent revisions of the C++ standard are trying to repair this with the new more-Java-like module system. – Brian61354270 Feb 25 '23 at 19:10
  • 1
    Welcome to Stack Overflow. Please learn that you are supposed to search before posting a question here. Since it appears that you didn’t find the linked original question it self, I might suspect that your search has not been that thorough in this case. – Ole V.V. Feb 25 '23 at 19:25

1 Answers1

4

I'm begining with java and I would like to know, if there's a possibility to declare a method (function) in class, but its body to place in another file

No. This is not how Java works.

because of better code arrangement.

This, arguably, does not provide "better code arrangement". It actually makes navigating C++ projects and writing C++ code more cumbersome since you have to jump around between header and implementation files.

If you want to declare something in one file and have an implementation in another file, then you would just follow best practices and have an interface and implementation of that interface.

// SomeInterface.java
interface SomeInterface {
    void doSomething();
}

// SomeImplementation.java
class SomeImplemenation implements SomeInterface {
    @Override
    void doSomething() {
        // Actually do something here
    }
}

// Main.java
static void main() {
    SomeInterface someInterface = new SomeImplementation();
    someInterface.doSomething();
}
dominicoder
  • 9,338
  • 1
  • 26
  • 32
  • Thanks for all the answers. It's interesting, for me it's much better to have more files, each for one function, than to have one big class with many functions bodies (I know i can hide them but still). – Radoslav Khun Feb 25 '23 at 20:13
  • Nothing's stopping you from having one function per file, you just have to wrap each function in a class because Java. However, that seems a bit extreme. The whole point of Java classes (and OOP in general) is to group related functionality into classes. This is why you have a Math class with multiple functions instead of a Min class and a Max class and a Round class, etc, each with their own single function. – dominicoder Feb 25 '23 at 21:21