2

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")

asked Sep 10, 2011 at 3:08
6
  • 3
    Like, perhaps. It looks more like code for an enum to me. Commented Sep 10, 2011 at 3:10
  • 1
    @Hovercraft Full Of Eels, +1 And enums are static by default. :D Commented Sep 10, 2011 at 3:12
  • @mre: indeed they are! a worthless +1 back atcha! :D Commented 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! Commented Sep 10, 2011 at 3:17
  • 1
    @Hovercraft Full Of Eels, Mmm...beer. Here, have another +1, cheers! :D Commented Sep 10, 2011 at 3:18

4 Answers 4

2

The NUMBER is static (and final) member of Foo class which return Foo object. Take a look at java.awt.Color class.

answered Sep 10, 2011 at 3:12
Sign up to request clarification or add additional context in comments.

5 Comments

Why isn't there a Foo number = new Foo() or something like that?
@bb2, Number could be a static instance.
@bb2 With java.awt.Color, you may use new keyword to create an object or you may get instance directly from the defined static FIELDS.
This is not necessarily final. If convention is followed it would be. (because constants are in all caps, and must be final to not be reassigned)
@ nicholas.hauschild Yeh agree!
2
class Foo {
 public static final Foo NUMBER = new Foo();
}
//later... 
Foo number = Foo.NUMBER;
answered Sep 10, 2011 at 3:15

Comments

1

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;
answered Sep 10, 2011 at 3:23

Comments

0
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.

answered Sep 10, 2011 at 3:17

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.