• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

Is there a difference between declararion and definition of a variable

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 441
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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".
 
Marshal
Posts: 77157
370
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I followed the link in the OP.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic