I'm having trouble understanding what this means, and how it was coded
Foo number = Foo.NUMBER;
Like, is Foo a class with static things?
(This was posted in an assignment it said "you might want to structure your solution like this")
-
3Like, perhaps. It looks more like code for an enum to me.Hovercraft Full Of Eels– Hovercraft Full Of Eels2011年09月10日 03:10:53 +00:00Commented Sep 10, 2011 at 3:10
-
1@Hovercraft Full Of Eels, +1 And enums are static by default. :Dmre– mre2011年09月10日 03:12:11 +00:00Commented Sep 10, 2011 at 3:12
-
@mre: indeed they are! a worthless +1 back atcha! :DHovercraft Full Of Eels– Hovercraft Full Of Eels2011年09月10日 03:12:42 +00:00Commented Sep 10, 2011 at 3:12
-
1@mre: well, actually all points are worthless, since you can no longer redeem them for beer and pizza. Cheers!Hovercraft Full Of Eels– Hovercraft Full Of Eels2011年09月10日 03:17:55 +00:00Commented Sep 10, 2011 at 3:17
-
1@Hovercraft Full Of Eels, Mmm...beer. Here, have another +1, cheers! :Dmre– mre2011年09月10日 03:18:34 +00:00Commented Sep 10, 2011 at 3:18
4 Answers 4
The NUMBER is static (and final) member of Foo class which return Foo object. Take a look at java.awt.Color class.
5 Comments
Number could be a static instance.class Foo {
public static final Foo NUMBER = new Foo();
}
//later...
Foo number = Foo.NUMBER;
Comments
As @Hovercraft pointed out in the comments, it's worth noting that this syntax could (probably?) be referring to enums:
public enum Foo { NUMBER }
Foo number = Foo.NUMBER;
Comments
class Foo {
public static Foo NUMBER = new Foo(); //might be final too...
public Foo {
// init Foo instance
}
}
Here is an example of a class definition that will allow the line of code you posted to compile. Foo is a class with a static member of type Foo identified by the name NUMBER.