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
Aurélie MEIJER
Java Web Development Techdegree Student 761 PointsGame game= new game("treehouse")
Hi everybody,
Could somebody explain what this command exactly mean word by word? i have seen that Game is the variable type of game ? how is that possible, normally its either bolean, int, string, etc ..... Can i use instead Game game = new game2("treehouse"); or Game game2 = new game("treehouse");
what should i change to do that ?
6 Answers
Simon Coates
28,695 PointsGame game= new Game("treehouse"); a variable game is declared of type Game (this means that there is a custom class called Game defined). The equals assigns what is on the right hand side to the left. new creates a new object, then Game indicates the type of object being created. the () enclose a parameter to be received by a constructor of the Game class. You can probably create other instances of the Game class using something like Game game2 = new Game("treehouse");
You can create variables of java's built-in types. The built in types can be primitives (int, bool, char) or objects. Apart from String (which is a special case), variables of object type need to use the new operator. You can also create custom object types by defining a class.
A trivial example is a Puppy class (stored in a file called Puppy.java)
public class Puppy{
public Puppy(String name){
// This constructor has one parameter, name.
System.out.println("Passed Name is :" + name );
}
}
to create an object of Puppy type, you might use:
Puppy myPuppy = new Puppy( "tommy" );
Puppy otherPuppy = new Puppy( "Rover" ); // another Puppy
Puppy yetAnotherPuppy = new Puppy( "Lassie" ); //Another Puppy
Aurélie MEIJER
Java Web Development Techdegree Student 761 PointsIn the Game class, we have this exact code:
public class Game { private String mAnswer; private String hit; private String miss; public Game(String answer) { IM TALKING ABOUT THIS one mAnswer=answer; hit=""; miss=""; } public boolean applyguess(char letter) { boolean isHit=mAnswer.indexOf(letter)>-1; if (IsHit) { hit+=letter; } else { miss+=letter; } } }
Does the word "Game" after "new" in the command refers to the methode Game highlighted above ? if i change to Game2 then is this the new command ?: Game game= new Game2("treehouse"); ?
Simon Coates
28,695 PointsObjectType variableName = new ObjectType(values of any parameters); the word new has the effect of create a new instance of an Object type. For example new Game(""), creates a new Game. The word Game is the type of object, but it is also the name of a special type of method that is called when a new object is created - a constructor. (the Object Type is Game as this follows the class keyword. The method game is a special method with a name that matches the class. Java knows to run the special method bases the methodName)
public class Game { //Game is the name of the class and thus, the name of the type.
private String mAnswer;
private String hit;
private String miss;
public Game(String answer) { //this is named for the class and runs when an object of this type is created. a constructor.
mAnswer=answer;
hit=""; miss="";
}
public boolean applyguess(char letter) {
boolean isHit=mAnswer.indexOf(letter)>-1;
if (IsHit) { hit+=letter; } else { miss+=letter;
}
}
}
Aurélie MEIJER
Java Web Development Techdegree Student 761 PointsOkey so your are saying that we must build constructors in the Game.java file with the name Game ?
Aurélie MEIJER
Java Web Development Techdegree Student 761 Pointsif we wanted to be executed , i cant change your code to this ? public class Puppy{
public Littledog(String name){ // This constructor has one parameter, name. System.out.println("Passed Name is :" + name ); } }
Simon Coates
28,695 Pointsconstructor has to match the name of the class, so you can't rename to Littledog. Java knows to look for something that matches the class name. It doesn't know to find Littledog.
Aurélie MEIJER
Java Web Development Techdegree Student 761 PointsThx simon you are so helpuful, i'm getting the idea. i have one other question if i take your code and add the following
public int age() { System.out.printLn("age is 7"); }
and i call using : Puppy myPuppy = new Puppy( "tommy" ); in another file java; it wouldnt display its age right since the constructor name is different from class ? if not how can i display his age ?
Simon Coates
28,695 Pointsi'm confused. Are you trying to give the puppy an age?
Aurélie MEIJER
Java Web Development Techdegree Student 761 Pointslets i inserted a method in Puppy.java under the name Bark that display the string "HAWHAW" like this:
Public class Puppy{
public Puppy(String name){
// This constructor has one parameter, name.
System.out.println("Passed Name is :" + name );
}
Public bark() {
System.out.printLn("HAWHAW");
}
}
then i called in another java file Puppy myPuppy = new Puppy( "tommy" ); will it display HAWHAW ?
Simon Coates
28,695 Pointshere's a slightly bigger example:
public class Puppy{
//instance variables go here
public int age;
public String name;
//constructor (a special type of method)
public Puppy(String nameParam, int ageParam ){
//store the name and age.
age = ageParam;
name = nameParam;
//output a string
System.out.println("Passed Name is " + name + " with age: "+age);
}
//this is a method
public void bark(){
System.out.println("Puppy with " + name + " and age: "+age+ " barks");
}
//this is a special method to run the class.
public static void main(String args){
Puppy aPuppy = new Puppy("Rover", 1);
aPuppy.bark(); //calls the method for aPuppy
}
}
When i run the main method, i get output of
Passed Name is Rover with age: 1
Puppy with Rover and age: 1 barks
Simon Coates
28,695 Pointsno. you need to call the method to get it to execute. Also the method needs a return value, so might be defined as:
public void bark() {
System.out.printLn("HAWHAW");
}
the void just means it doesn't return a value.
Aurélie MEIJER
Java Web Development Techdegree Student 761 PointsThank you , now i get it . your 're awesome SIMON !!!!!!!!
Simon Coates
28,695 Pointsglad to help.