Difference Between Definition and Declaration in Java



For the difference between definition and declaration, one should consider their literal meaning first which includes Declare means to announce or proclaim while Define means to describe some entity.

The following are the important differences between the Definition and the Declaration.

Sr. No. Key Declaration Definition
1 Concept The concept of declaration includes informing the compiler about properties of the variable such as its name, type of value it holds and the initial value if any it takes. While the definition is basically the actual implementation and memory location of function and about memory for the variable is allocated during the definition of the variable.
2 Exception in C Both declaration and definition take place at the same time in the case of c language. In other languages such as Java both occur at different places.
3 Number of occurrences The declaration could be done multiple times either of a variable or of function. Variable or function could be defined only once.
4 Memory allocation Memory has not been allocated during the declaration of a variable or function. Memory has been allocated during the definition of a variable or function.

Example of Declaration vs Definition

JavaTester.java

 Live Demo

public class JavaTester{
   public static void main(String args[]){
      int a; // declaration of variable
      a=10; // definition of variable
      functionA(a); // declaration of function
   }
   public static void functionA(int a){
      System.out.println("value of a is " + a); // definition of function
   }
}

Output

value of a is 10
Updated on: 2019-09-18T12:14:17+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements