Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Understanding object creation  RSS feed

 
Lynne Dixon
Greenhorn
Posts: 9
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm working through Head First Java and trying to wrap my head around the mechanics of object creation. I'm pulling this example from that book, pages 38-39.

The example shows 3 classes:

Player
GuessGame
GameLauncher

Player has an instance variable "number", and a method guess().
GuessGame has instance variables "p1, "p2", and "p3", and a method startGame() that runs the game.
GameLauncher contains the main().

At the start of GuessGame's startGame(), there is this:



My confusion in object creation is the repetition of the name, in this case Player and Player().

Are the "Player p1" and "p1 = new Player()" both referring to Player.class?

Here's another example:

Dog d = new Dog();

I'm confused as to there being a "Dog" type and a "Dog()" method of the same name, doing different things.

Thanks for your help
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66205
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dog is the class. Dog() is the constructor for that class. Constructors are always named for the classes that they construct.
 
Lynne Dixon
Greenhorn
Posts: 9
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't learned about constructors, yet. Does the JVM create the constructor? Where does it come from?
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If a class has no explicit constructor, then it is assumed that there is a no-argument constructor with an empty body, as if you typed this:

In this case, the class would be Dog. "Dog" would change if the class was named something else. You can read more about it here.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lynne Dixon wrote:Are the "Player p1" and "p1 = new Player()" both referring to Player.class?

Yes and no.

Once you create a class called Player, the compiler will assume that any time it sees the word 'Player' in open code (ie, not in quotes or in a comment, or as part of some larger word) it means a Player type or object - including inside the Player class itself.

However, what it does with that knowledge depends on the context.
  • In the case of "Player p1" it is the declaration of a variable (called 'p1') that can hold a reference to a Player object.
  • In the case of "p1 = new Player()" it is a statement that assigns the reference of a newly-created Player object to p1.

  • HIH

    Winston
     
    Lynne Dixon
    Greenhorn
    Posts: 9
    Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    So in my first example, "Player p1" refers to the class Player. And "p1 = new Player()" refers to the constructor Player. Correct?
     
    Knute Snortum
    Sheriff
    Posts: 4073
    112
    Chrome Eclipse IDE Java Postgres Database VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Player p1; defines the variable p1.
    p1 = new Player(); assigns the object created with the Player() constructor to p1.
     
    Winston Gutkowski
    Bartender
    Posts: 10573
    65
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Lynne Dixon wrote:So in my first example, "Player p1" refers to the class Player. And "p1 = new Player()" refers to the constructor Player. Correct?

    Basically: Yes.

    Winston
     
    Lynne Dixon
    Greenhorn
    Posts: 9
    Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Awesome. Thanks all
     
    Winston Gutkowski
    Bartender
    Posts: 10573
    65
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Lynne Dixon wrote:Awesome. Thanks all

    You're most welcome.

    And welcome to JavaRanch!

    Winston
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!