Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Objects Creating the MVP Getting Started

What does new do?

at 4:15 he wrote Game game = new Game("treehouse"); please explain how this code block works.

1 Answer

If you look at his files, he has a file called Game.java. In that file he made a constructor.

In the other file, he typed in:

Game game = new Game(whatever he typed in...);

The Game.java file is a class. You can think of a class as a blueprint. The Game.java file doesn't do anything. It is just a blueprint. It says the instructions for whenever someone creates a NEW Game.

In the main file, he typed in the line of code(above). Game refers to the game.java file. game(lowercase) is what he is naming this VARIABLE.

Remember when you create a variable with the String data type you would do this:

String myString = "my lovely string";

He is doing the same thing, except the data type is GAME. He made a NEW GAME. When he types in NEW GAME(); he is creating a GAME OBJECT following the Game.java BLUEPRINT. (The computer does it, he just needs to type in new Game ;)) The new Game Object he made is now stored in the game variable.

I hope this helps!

If you have anymore questions, please reply.

Thanks! :)

Thanks, I get it now!