Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between declaration and definition in Java?

Tags:

java

I'm very confused between the two terms. I checked on stackoverflow and there's a similar question for C++ but not for java.

Can someone explain the difference between the two terms for java?

like image 991
Alan2 Avatar asked Jul 30 '12 04:07

Alan2


People also ask

What is the difference between definition and declaration?

Difference Between Definition and Declaration It aims at determining the overall values stored in a class, a function, or a variable. It aims at specifying the name of any given class, function, variable, etc. Definition allocates memory to an entity. A declaration does not allocate memory to the entities.

What's the difference between function definition and declaration?

Function declaration is a prototype that specifies the function name, return types and parameters without the function body. Function Definition, on the other hand, refers to the actual function that specifies the function name, return types and parameters with the function body.

What is method definition and declaration in Java?

Like a class, a method definition has two major parts: the method declaration and the method body. The method declaration defines all the method's attributes, such as access level, return type, name, and arguments, as shown in the following figure. The method body is where all the action takes place.

What is difference between class declaration and class definition?

A class definition is a process of naming a class and data variables and interface operations of the class. A class declaration specifies the representation of objects of the class and set of operations that can be applied to such objects.


2 Answers

The conceptual difference is simple:

  • Declaration: You are declaring that something exists, such as a class, function or variable. You don't say anything about what that class or function looks like, you just say that it exists.

  • Definition: You define how something is implemented, such as a class, function or variable, i.e. you say what it actually is.

In Java, there is little difference between the two, and formally speaking, a declaration includes not only the identifier, but also it's definition. Here is how I personally interpret the terms in detail:

  • Classes: Java doesn't really separate declarations and definitions as C++ does (in header and cpp files). You define them at the point where you declare them.

  • Functions: When you're writing an interface (or an abstract class), you could say that you're declaring a function, without defining it. Ordinary functions however, are always defined right where they are declared. See the body of the function as its definition if you like.

  • Variables: A variable declaration could look like this:

      int x; 

(you're declaring that a variable x exists and has type int) either if it's a local variable or member field. In Java, there's no information left about x to define, except possible what values it shall hold, which is determined by the assignments to it.

Here's a rough summary of how I use the terms:

abstract class SomeClass {                // class decl.                                           //                           \     int x;                                // variable decl.            |                                           //                           |     public abstract void someMethod();    // function decl.            |                                           //                           |     public int someOtherMethod() {        // function decl.            |                                           //                           | class         if (Math.random() > .5)           // \                         | def.             return x;                     //  |  function definition   |         else                              //  |                        |             return -x;                    // /                         |                                           //                           |     }                                     //                           | }                                         //                          / 
like image 115
aioobe Avatar answered Oct 03 '22 22:10

aioobe


The Java Language Specification specifies and uses the term "declaration" extensively, but it does not use "definition" except as a normal English word.

My evidence is that the term "declaration" appears a number of times in the JLS table of contents and in the index. By contrast, the word "definition" does not appear in either.

So, when you see someone use the word "definition" in the context of Java, they are either using it in the non-technical sense, or they are being sloppy with their terminology.

In the latter case, they might mean the same thing as the technical term "declaration", or they might mean something else. And if they mean something else, you need to ask them what they mean. If they have defined it ... fair enough, but it is not standard terminology.


The answers that state that "definition" refers to the point at which the variable is initialized are specifically not supportable ... in the context of Java. In Java, initialization of a variable either happens at the point of declaration, or in a later assignment. In the latter case, no special term is used ... or needed ... apart from assignment and / or initialization. There is no specified point at which storage is allocated for the variable. Indeed, the chances are that the space for the variable itself is allocated before the declaration is reached.


The reason that the "definition" term is not used in Java in the JLS spec is that it is not needed.

  • Since Java allows members to be declared in any order, there is no need for "forward declarations". That is the context where there it is necessary to distinguish between the two concepts.
  • In Java the stack space required for a variable is a compile time constant, so the stack offset calculations happen at compile time. (Remember that in Java, an array is a reference to a heap object ... and only the reference is held in the stack frame.)
  • The way that Java handles "definition without initialization" of a field or variable does not require a single "declaration" point. If an initialization of a variable is required, it may happen at multiple points in the source code.

(The only place in Java where they might have used declaration versus definition is in abstract methods. Except that if they had done, the would have had to refer to a regular method declaration as a definition ... for consistency ... and that would be confusing. So they just call the "abstract" subcase an declaration of an abstract method.)

C and C++ handle these things differently, and hence do need distinct "declaration" and "definition" terms in their technical descriptions. My take on the "Sun Glossary" definitions are that they are C / C++ -centric.

like image 30
Stephen C Avatar answered Oct 03 '22 23:10

Stephen C