wood burning stoves 2.0*
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
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: 14074
    
  16

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 7 API documentation
Scala Notes - My blog about Scala
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37976
    
  22
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: 37976
    
  22
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!
 
 
subject: a variable is used before declaration in enum and its running ok how
 
Similar Threads
Enum Constant Specific Class Body + private member
How are enum constructors initialised
confusion regardind enum and constructors
Enums (super call)