1

I was under the impression that a new object can be created if there is a existing class for it.

public final class Suit implements Comparable {
  private String name;
  private String symbol; 

  public final static Suit CLUBS = new Suit( "Clubs", "c" );

How does this work? What is the benefit of initializing within its own class as opposed to doing it in the main?

Thanks in advance.

WaveRunner
  • 67
  • 3
  • 5
  • This question has a pretty good answer here: http://stackoverflow.com/questions/18360891/how-does-creating-a-instance-of-class-inside-of-the-class-itself-works – AgentX Nov 20 '15 at 05:45

4 Answers4

2

a class is itself an object, that implements methods - such as newInstance() which gets you an object of type class. I know that wasn't the clearestDefinition but here's kind of how it works:

Whenever you create a new Object of type MyClass, the classloader first retrieves and creates an object that represents your class:

Class<?> MyClassObject

which performs a silent construction of all your 'static' variables. The program then asks MyClassObject for an instance of MyClass:

MyClass object = MyClassObject.newInstance()

static variables and methods belong to the MyClassObject, whereas instance variables belong to MyClass

JRaymond
  • 11,625
  • 5
  • 37
  • 40
1

Notice CLUBS is static. It is not part of any object but belongs to the class as a whole.

You could have initialized CLUBS in main, but then

  1. CLUBS would only be visible within the main method
  2. If you run java without a main method (e.g. web page there would be no CLUBS)
emory
  • 10,725
  • 2
  • 30
  • 58
  • 1. is true only if you don't discriminate "declare" from "initialize". But who knows, OP might have meant just this. – Marko Topolnik Apr 20 '12 at 22:37
  • True. I assumed the OP mean declare and initialize. If the OP meant only initialize then the OP would have to remove the final modifier - which may be OK depending on the OP's goals. – emory Apr 20 '12 at 22:40
0

There are no any issues, because at the moment of instantiation whole information about the class structure is available and it can intantiated (no matter in a static or non-static field).

It's something like a recursive definition - which is totally legal:

   Node:
      Node parent;
      Node left;
      Node right;
Eugene Retunsky
  • 13,009
  • 4
  • 52
  • 55
0

This is a classic scenario for a typesafe enum (pre-Java 1.5) or for a singleton class. The benefit of initializing at the declaration site is that this is the only way for the field to be final, which is itself a very important characteristic.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • I am hearing about enums. Would this be a case where using enums makes more sense? – WaveRunner Apr 20 '12 at 22:35
  • Sure thing. Since Java 1.5 `enum` is the right way to achieve a typesafe enum, as well as a singleton. – Marko Topolnik Apr 20 '12 at 22:38
  • It probably makes sense to use `enum` here. In the unlikely event you want to retain the ability to make up your own nonstandard Suites at runtime (e.g. Let the user determine the Suite names of your nonstandard deck of cards). then it does not make sense. – emory Apr 20 '12 at 22:42
  • Suits are the example used for enums in Effective Java. – Marko Topolnik Apr 21 '12 at 05:40