aspose file tools*
The moose likes Beginning Java and the fly likes Is there a difference between declararion and definition of a variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Is there a difference between declararion and definition of a variable" Watch "Is there a difference between declararion and definition of a variable" New topic
Author

Is there a difference between declararion and definition of a variable

John Phoenix
Greenhorn

Joined: Jun 08, 2011
Posts: 4
Hello,

I'm a little bit confused. In C there is a difference between the declaration and the definition of a variable. In a (german) Java beginners book is stated that a variable declaration is always connected with storage allocation so there is no differentiation between declaration and definition.

So I googled a little bit.

http://java.sun.com/docs/glossary.html#D

In the glossary here are the two definitions differentiated. So is there a official differentiation (e.g. in the language specification) between declaration and definition or is a declaration in Java always a definition, i.e. connected with storage association.

I found no clear statement that there is no definition in Java and always a declaration.

regards
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

I am not sure what the official stance is in Java, but I have always considered them different. This is the definition I always use:

"A declaration introduces a name and indicates its scope, but may omit implementation details."

"A definition describes the object in sufficient detail for the compiler to determine its implementation."

both are from, Programming Language Pragmatics by Michael Scott, it should be on google books.

Hunter


"If the facts don't fit the theory, get new facts" --Albert Einstein
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

To give the Sun glossary's definitions of the words:
declaration
A statement that establishes an identifier and associates attributes with it, without necessarily reserving its storage (for data) or providing the implementation (for methods). See also definition.

definition
A declaration that reserves storage (for data) or provides implementation (for methods). See also declaration.

The way I read the Sun glossary would be like this:
The wording of the second seems a bit strange, since the second line above is not a declaration - it's an object instantiation and variable assignment. In Java, you don't "reserve storage" (except maybe for arrays) - you create objects, which is often part of the same statement as the declaration, but isn't the same thing.

I don't recall anyone ever talking about variable "definition". It makes more sense for methods though, which can be declared in an abstract class or interface, but even then you talk about "implementation" rather than "definition".
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39784
    
  28
. . . and welcome to the Ranch

You are doing better than me with those Sun glossary terms, Luigi Plinge; I find the difference hard to understand. Where did yo ufind that glossary; I went through the Java™ Language Specification index and couldn't find a heading for "definition".
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

I followed the link in the OP.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4461
    
    8

In terms of methods, maybe it's a hangover from C/C++, where you have declarations in a header file and a separate definition.

In Java, I'd probably only use the word "definition" when defining a class.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is there a difference between declararion and definition of a variable