aspose file tools*
The moose likes Java in General and the fly likes a variable is used before declaration in enum and its running ok how Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "a variable is used before declaration in enum and its running ok how" Watch "a variable is used before declaration in enum and its running ok how" New topic
Author

a variable is used before declaration in enum and its running ok how

kapilg gupta
Greenhorn

Joined: Jun 14, 2006
Posts: 10
enum Coffesize{

BIG(8),HUGE(10);
Cofeesize (int ounces)
{
this.ounces=ounces;
}
private int ounces;
public int getOunces()
{return ounces;
}
}
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14272
    
  21

Member variables don't have to be declared before they are used. As long as the member variable is declared in the class (or enum) somewhere, it will work.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Member variables don't have to be declared before they are used.


Agree. In fact, I think, there is no order of declarations or methods. There is a conventional order we write things down in, but the order of declarations ro methods does not affect the compilation of a class.

The only place where order matters (I think) is inside a method.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
The only place where order matters (I think) is inside a method.


Sorry, I forgot: A package statement has to precede your import statements which precede your class declaration.
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Originally posted by Campbell Ritchie:

The only place where order matters (I think) is inside a method.


Static initializers and expressions used to initialize static fields are executed in their listed order:


Ditto for instance initializers and instance fields:


There is no emoticon for what I am feeling!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: a variable is used before declaration in enum and its running ok how