• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Declaration vs Definition

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Different texts use these words interchangeably? What is the difference though in the following contexts : variable, method , class,interface.
 
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Raj,

The following is a variable declaration:

int x;

The following is a variable definition:

int x = 10;

The following are method declarations:

abstract void foo();
native void foo();

[editing 20 Jan 2008]

method definitions is when what the method does is defined:

void foo() {
System.out.println("Hello");
}

In interfaces you can define variables (but they are really all constants). In interfaces you can only declare methods, but not define them.

In abstract classes, some methods may be only declared and others declared and defined.

In concrete classes, all methods must be defined (except native methods) which are defined in a language such as C.
[ January 20, 2008: Message edited by: Kaydell Leavitt ]
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Declaration -- you declare or intimate about something. It is like half the way you say that this particular entity/thing will be of this specific kind.

Definition -- this is like you complete/fulfill the information about what this entity should contain or behave.

In case of variables, you say that this is the variable of this kind (datatpe) and this holds a particular value (say 10 for example). [int x = 10;]

In case of methods, you say that this method should behave like this. You are providing the implementation code about how it can actually *work*.

Class/Interfaces - I think what Kaydell said is right. According to the way Declaration Vs Definition goes.

Because in an Interface, you will always declare the methods and in the implementing classes you should provide the implementation for them (rather you define the methods).

But in abstract class, it is NOT mandatory that you should only declare the methods as a class with no abstract methods is perfectly legal and eligible to be called as an abstract class.

Does that help?
 
Viralraj Upadhyay
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yep! This is just what i knew about definition and declaration. But the ambiguity stems when i read such a thing on sun tutorial. The question is though the class has implementation details why it then called a declaration.


Also unlike C++ in java method declarations are quite rare in classes except for interfaces and abstract classes. Most of the times it is the definition only straight away.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A declaration is called a declaration whether it has an implementation or not. Your code has a declaration for the class MountainBike, a declaration for the field seatHeight, a declarations for a MountainBike constructor, aa declaration for the method setHeight, and declarations for five different method parameters.

As for "definition", that isn't formally defined in the JLS. Usually I would agree it refers to an implmentation. E.g. an abstract method is a declaration but not a definition, while a concrete method is a declaration and a definition. I would not use the term "definition" to refer to a variable though, unless the variable was a constant. Otherwise I might say the variable is initialized, or not initialized. However I don't know if others would use the term the same way. I'm not sure it really matters. The term "definition" is too vague here. If you need to discuss it, I'd use more specific terms like "method body" or "variable initializer", depending which you're talking about.
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jim Yingst:
...I would not use the term "definition" to refer to a variable though, unless the variable was a constant. Otherwise I might say the variable is initialized, or not initialized. However I don't know if others would use the term the same way. I'm not sure it really matters. The term "definition" is too vague here. If you need to discuss it, I'd use more specific terms like "method body" or "variable initializer", depending which you're talking about.



This is perfect! I too agree with you Jim

But somehow I have got used to say "variable is defined" in place of "variable is initialized".

I don't think it is applicable. Aint I?
 
He got surgery to replace his foot with a pig. He said it was because of this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic