permaculture playing cards*
The moose likes Beginning Java and the fly likes Declaration vs Definition Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Declaration vs Definition" Watch "Declaration vs Definition" New topic
Author

Declaration vs Definition

Viralraj Upadhyay
Greenhorn

Joined: Nov 10, 2006
Posts: 22
Different texts use these words interchangeably? What is the difference though in the following contexts : variable, method , class,interface.
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 688

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 ]
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

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?


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Viralraj Upadhyay
Greenhorn

Joined: Nov 10, 2006
Posts: 22
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.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

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?
 
jQuery in Action, 2nd edition
 
subject: Declaration vs Definition
 
Similar Threads
Clarification
WA #1.....word association
Value Object v. Transfer Object
interview question
Array Initilaize