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 trialChaz Hall
1,970 PointsObjects Clarification
When writing a program in java you use objects. It seems there's always one object that pulls all of the puzzle pieces together and gives us the entire program through it. Is this the correct understanding? If so:
-Declaring something under a public object is available for the entire program unless it is declared private which means it is only usable by the object's classes. Member variables are variables that are used only in that Object and begin with m ("mSuperSoaker").
- Constructor:
I don't fully understand this, but my current line of thinking is that in the programs main object:
public static void main(String[] args){ Game game=new Game("treehouse");
is available to create a reference to another object that has it's own classes and such that this main part of the program accesses. When using the "new" keyword it tells the Game object (in this case) to be told "Get to work!" and the code runs and is sent back to the Main part of the program.
Passing: When passing that just means sending the information to a undeclared variable? Does it mean passing it to any part of the class/objects/objects depending on what we tell it to do?
Thanks for all of the help!
2 Answers
Josip Dorvak
18,126 PointsIm not taking this course so I don't know the specifics of what your program should do but I'll try to answer as much as I can.
A class is sort of like a blueprint. It list all the functions and variables(attributes) that it contains. An object is a physical representation of a class. You create objects of a class, similar to a building is the object and the blueprint is the class. A constructor is a function that will run when an object gets created. If you do not have a constructor, a default one is created for you(will simply set everything to their default values i.e false, 0, null). So a constructor will always run. A constructor lets you set values or setup an object as it gets created. When you say new Game("example"); you are creating a Game object and passing the string "example" to the constructor.
Also main() is not an object, but a method. It is simply the starting point of any application. However most functions must be called by a certain object, however since your program is stating, no objects are created. This is why main is static. Static methods are able to be called without any created objects.
Hope this helped
iftekhar uddin
4,194 Pointsthe keyword "new" is something that allocates space for an Object to be used. (ask if you want me to go further)
An object is created to hold values for...well...an object. Like how a iceCreamTruck has ice cream. But they have certain amounts of ice cream.
So if you make an Object (making a class to write out this object) of IceCreamTruck then a variable in there could be amountOfIceCream.
Now you don't just get an ice cream truck and go places without loading ice cream do you? Maybe you do. That's where constructors come in. This is when you can get trucks with default amount of ice cream (the default constructor) or you can (pass in a variable to the constructor (method)) like
IceCreamTruck truck = new IceCreamTruck(50);
To add 50 gallons of ice cream to the ice cream truck. That 50 in the amount passed into the method to construct the object.
Lets say that on default you just want 5 gallons of ice cream in each one. So you'd create the constructor like that.
public IceCreamTruck() { iceCreamAmount = 5; }
and call it using:
IceCreamTruck truck = new IceCreamTruck();
I hope this helps somewhat :D