Hi,
In C program Function has three parts,
Function Declaration.
Function Definition.
Function Calling.

In java i cannot able to declare a function .Is it possible to declare a function in Java.

For example:

import java.io.*;
import java.util.*;
class Fun_example
{
//	show();//Cannot able to Declare a Function ?

	public void show(){ //Function Definition
		System.out.println("Sample Method ");
	}
	
	public static void main(String args[])throws IOException{
		ex_constructor obj1,obj2;
		obj1=new ex_constructor();
		obj1.show();//Function Calling
	}

}

Thank you,

With Regards,
Prem

You may declare a method (in java we call function as method) as abstract which means no definition is given, e.g.

void show();

You may declare a method with empty body, which isn't an abstract method anymore.

void show(){};

but do nothing when calling it.
Or you may define a method with its body

public void show(){ //method Definition		
System.out.println("Sample Method ");	
}

Hi,
It is not possible to declare,

void show(); 
void show(){}

For ex:

   import java.io.*;
      import java.util.*;
      class Fun_example
      {
       // show();//Cannot able to Declare a Function ?

       void show();//Error while declaring
       void show(){};//Error while declaring             

        public void show(){ //Function Definition
     System.out.println("Sample Method ");
     }
      public static void main(String args[])throws IOException{
       ex_constructor obj1,obj2;
       obj1=new ex_constructor();
       obj1.show();//Function Calling
       }
      }

prem2,
Thank you for your quick response. Do you mean in my code, I can not place abstract method show() in it. If so I agree with you.
How do we say:
For the interface MouseListener there are 5 abstract methods declared:
void mouseClicked(MouseEvent e)
void mouseEntered(MouseEvent e)
void mouseExited(MouseEvent e)
void mousePressed(MouseEvent e)
void mouseReleased(MouseEvent e)
I wounder if the word "declared" used here is valid or not.
You may define an interface or abstract class where an abstract method void show() exists. If you are going to declare an abstract method void show() , for example, in an abstract class, you have to place the keyword abstract at first. Of course, in this case one may not have any instance from it. That's why in your/mine program, if an abstract method is declared, it cann't have object from it, i.e. it doen't work.

So, we can declare the method in abstract or interface.Is there any other option in java to declare the method .

Thank you,

Prem - are you looking for function prototypes in Java? If so, there are none.
In C, you have a header file with

int foo(int dollars);
float bar(void);

and so forth. This is neither necessary nor possible in Java. This may be too much detail, but this is because Java's compiler does two passes. The first pass is a partial descent, it just identifies the methods (and their fields? I can't recall) so that the second pass can know whether the methods are used correctly.

This is how it's possible, for example, to have a Node class that calls on other Nodes. The Node doesn't have to finish compiling for the compiler to know that it's there and can be used as a declaration. You can also have circular references - Class A has a field of Class B, which has a field of Class C, which has a field of Class A - and so forth. With a one-pass compiler, this is only possible if you use function prototypes as a promise to the compiler that a given function will exist. However, with a two-pass compiler, the prototypes are not needed.

First of all, functions are called methods in java and the declaration and definition of a method in java is same as in c but here calling of methods is done with help of objects of classes.Function declaration can also be done in abstract classes and in interfaces (in case u want seprate declaration and definition).
Corrected code of your programe is given below:

public class Fun_example
{
 public void show()
        { //Function declaration and Definition
		System.out.println("Sample Method ");
	}
 
 public static void main(String args[])         //main method
       {
		Fun_example obj=new Fun_example(); //creating object with new keyword
		obj.show();                       //Function Call
	}
 
}

I haven't really thought of interfaces as header files before, but it does make some of sense. The parallel is reasonable, but I think it might be misleading in the end. The real purpose of an interface is not to declare a method (which is not necessary) but to require that method. Interfaces are about interaction of objects, and specifying what sorts of interaction are allowed, not about helping the compiler to do its job. In fact, you could eliminate interfaces from Java and not affect the way the compiler handles methods. What would change would be the ability to determine that a given object is capable of (for example) providing access to its contents in sequential fashion (ie, is Iterable). This is not the case with header files and prototypes in C, which are what I think of when I read "separate declaration and definition".
So I think it's a mistake to describe interfaces in terms of "separate declaration and definition" - that's really not what they're doing.

In a few sentence, in Java, you don't need to declare a Function first and then define it and then invoke it. You can simply define it and use it.

However, if you still want to declare function, then you can use Interface, where you declare your function and then implement that Interface.

However, if you still want to declare function, then you can use Interface, where you declare your function and then implement that Interface.

@Java Programmer - What does this mean? What does it mean to declare a "function" in Java, and why would you do it?

Hi,
So, the final solution is We cannot able to declare the function in java.
The declaration can be done only in interface and abstract.
If the above statements are wrong please clarify it.

Thank you,
Prem

I don't think so. If by "declaration" you mean, as you suggest in the first post in this thread, something like the function prototype in C, no such thing is ever required in Java. It's not that you "cannot" do it, it's that the idea doesn't exist. As I said above, this is due to the fact that the java compiler is a two-pass compiler.

It sounds to me like you want to use interfaces or abstract classes as something like C header files. This makes no sense - they might look a little like header files, but that's a coincidence. They aren't even vaguely similar.

That's my understanding of the situation, in any case. I may have misunderstood what you mean by "declaration", but if you mean something like C, then no, the category doesn't apply, period. That's an artifact of a different compiler. It's like asking how to replace a vacuum tube on the JVM.

You can simply declare function abstract. It is a valid statement in java.
void show(){};

That's not declaring an abstract method. Please read the whole thread before posting - it you had then you would know that you have defined an empty method (definition is complete, it can be called, but it does nothing). To declare an abstract method you have to either use the abstract keyword or declare the method as part of an interface definition, where methods are abstract by default.

Note also that his thread has beed dead for 9 years, so its unlikely that anyone is still waiting for the answer!

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.