• Post Reply Bookmark Topic Watch Topic
  • New Topic

difference between declaration and defination  RSS feed

 
Archies Gomes
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Can someone pls give the very basic difference between the declaration and the defination of a variable with ex.....Tx.
 
Steve McCann
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean declaration and instantiation?
You can declare a variable at any time, but the object is not created until it is instantiated.
A variable can be declared for a File object, but the file object does not exist as it has not been instantiated (or created):

The File object can then be instantiated (created) with this line:

Only after this does the object exist
 
Shyam Prasad Murarka
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Readers,
I hope that what I say is clear because if it is not clear then please forgive me for my poor teaching skills. BUT if you find it useful then at least reply back saying that it was useful.

Ok, let's say you want an apple and you say that "I want an apple!". This is similar to saying that you want a variable and therefore declare it.
BUT Remember that by just saying you want an apple you won't get it. Similarly, no memory space will be reserved for that variable by just simply declaring it. You will have to go and buy the apple which is similar to instantiating a variable by giving it some value (like you give money for that apple).

I just tried explaining it in a different style. Please forgive me if you got more confused by reading this.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12562
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Back in my C programming days, there was a difference between declaring something and defining something.

in C, you could have a structure, which is like a class without methods. you would do something like (and i've probably got the syntax wrong now)


at this point, the compiler now understands what a point is, but none exist. You have DEFINED what it means.

once i have done that, i can DECLARE one - i.e. make one...



this would then create the space in memory to hold two ints, and in this case, intialize the x-val to 2, and the y-val to 3.

at least, i think that's correct... it's been a few years since i've written any C.
 
Archies Gomes
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah thanks Shyam and Fred
Fred you really pin pointed it very wonderfully.....i was actually just thinking bout "C" when i posted this query....i think you hit it the right way....Thanks
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A method declaration just says that there is a method with a specific signatures. A methode definition defines what that method does.

An interface, for example, only contains method *declarations*. A method *definition* is the methods code.

At least that's my understanding...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!