• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Understanding object creation

 
Greenhorn
Posts: 9
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I haven't learned about constructors, yet. Does the JVM create the constructor? Where does it come from?
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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: 7125
    184
    Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Awesome. Thanks all
     
    Winston Gutkowski
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Lynne Dixon wrote:Awesome. Thanks all


    You're most welcome.

    And welcome to JavaRanch!

    Winston
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic