Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is there a difference between declararion and definition of a variable

 
John Phoenix
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49447
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . 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
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I followed the link in the OP.
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic