hello i just get started with java so i have a question where the difference from c/c++ and the famous lovely java begin, the function or better method, so in c/c++ we can make a function prototype on top the main and then define it later like this

int myfunctionprototype(int arg1, int arg2);//function prototype
 int main (void){
 callfunction = myfunctionprototype( x, y);
 }
 int myfunctionprototype(int arg1, int arg2){
 and here define what to do;
 return result;
 }

i have undestand all structure of java methode but i only want to know if all method my be declared on the top and define there.

Dani AI

Generated

Short answer (for a C/C++ programmer): Java does not have standalone function prototypes. Methods always live inside classes or interfaces. When only a signature is needed (the "declare but define later" pattern), use an interface or an abstract class; otherwise declare and define the method together in the class. This is the practical equivalent of the C/C++ prototype/implementation split mentioned by , and it aligns with points made by , and about interfaces, static methods and the class-centric model.

Example: an interface (signature only) and its implementation in separate files.

/* Calculator.java */
public interface Calculator {
    int add(int a, int b);
}

/* SimpleCalculator.java */
public class SimpleCalculator implements Calculator {
    @Override
    public int add(int a, int b) {
        return a + b;
    }
}

/* App.java */
public class App {
    public static void main(String[] args) {
        Calculator c = new SimpleCalculator();
        System.out.println(c.add(2, 3));
    }
}

Utility-style static methods (roughly analogous to C-style global functions) go into a final class with a private constructor:

public final class Utils {
    private Utils() {}
    public static int sum(int... values) {
        int s = 0;
        for (int v : values) s += v;
        return s;
    }
}

Practical notes: method order in a class does not matter; a public top-level class must live in a file with the same name; put interfaces or abstract declarations in their own .java files to separate API from implementation; if the compiler reports "cannot find symbol," verify package names, filenames, and that all relevant .class files are on the classpath. Also be aware that since Java 8 interfaces can include default and static method bodies, so interfaces are no longer strictly signature-only in every case.

Recommended Answers

All 6 Replies

I don't know c++, but maybe the Jave equivalent would be to declare an interface. Interfaces contain method signatures but not their implementation (Java 8 changed that a but, but let's not worry about it now). Then when you later define a class you declare the class as implementing that interface, and supply a complete method implementation. You typically do that so other code can make calls to the method without needing to know what the implementing class is. Eg the List interface has method signatures for adding removing, iterating etc on a list. The actual methods are defined in classes like ArrayList or LinkedList that implement List.
Having said that, the order in which methods are declared is irrelevant in Java, so there's no need for a prototype just because the definition comes later in the source code.

In Java you first have to import (similar to #include) the package that defines the method. So no, there is no function prototype AFAIK in Java.

Also, in Java, all methods are part of a class, and may declared in an interface class which has to be implemented somewhere before you can use it. If this is a static method of a class, then you can call it as classname.methodname(arglist);

Sometimes we create a class that only has static methods for general procedural functions. Call it MyStaticClass with static methods such as myfunction(int,int), so in your startup class main() function, you would call it as (assuming it returns an int)int results = MyStaticClass.myfunction(x,y);

I don't do much programming in Java. But here's how I think about it, though it's bound to be off somewhere.

In Java each function is a class. It has a name, declaration, and a body. It is declared using public static void name of function(parameterList) followed by a function body. Each function can have a main() function so it can be run as an independent program, if desired.

In C++ functions can be independent from a class or they can be members of a class (when they are called methods). Functions/methods do NOT contain a main() function as in Java, and cannot be run idependently. They always need to be run as part a program, which can only have ONE main() function.

Now, I don't know enough Java to know if all functions need to be declared with the keywords public, static and void. The keywords, public and static, are only valid if the function is a method. The keyword public could be replaced by keyword(s) private (and protected ?). Now, I don't know enough Java to know if all functions need to be declared with the keywords public, static and void. In C++ the keyword , void, could be any valid type.

I think of Interfaces in Java as the "equivalent" of an Abstract Class in C++.

In the Java I've seen, the function body has always immediately followed the declaration. Whereas in C++, if the function is an independent function, the function needs to be declared before main(), but the definition (where the body of function appears) can before or after main(). If the function is declared before main() but defined after main() , then it is sometimes called a prototype. When the function is a method in a Class it is sometimes declared and defined when the Class is declared within the program where it is used, and sometimes it's declared in a header file, in which case it can be defined in the header class, too (if it's small, if it's inline, or if it's static); but it is usually defined in the .cpp file.

You may get other (better advice on the differences) from someone else, but this gives you a broad outlines of the similarities and differences.

commented: thanks very helpfull to see the explaining that in java the function body allways follows the declaration +2

Hi Lerner
What you say is mostly right, it's just your terminology is not always standard Java usage. ;)
Just for the record, here are a couple of notes...
You may be confusing the terms function and class? Java doesn't have things called functions, just methods. All methods are defined inside a class. A class may or may not have a method called main. In any program at least one class must have a public static void main method, which is used as the entry point for the program. In general things in Java can be public / protected / private, and static or not, depending on how the program is designed. The only exception is the entry point main method, that must be public and static and void.

Rubber man: small point: import statements are always optional, they are never required. Their raison d'etre is to save you having to use a fully qualified name in your code. Eg, ArrayList is part of the java.util package, so you refer to it as java.util.ArrayList . When that gets boring you import java.util.ArrayList or java.util.*, which allows the compiler to qualify the name for you. Importing a whole package's names can be dangerous when two packages have classes with the same name and you have imported all the names in those packages - so they are not always a good idea.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.