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 trialRaju Rajpurohit
544 PointsI want to know more about the classes and objects
I want to know more about the classes, datatype and objects with android.
2 Answers
Aaron Arkie
5,345 PointsI'm not familiar with PHP but i hope to learn soon! However, if it is like javascript it's sort of similar where you just declare a variable and the computer figures out what type it is by itself. In answer to your question:
Vehicle in this case is the data-type of the object!
so we can substitute the formula:
data-type variableName = ?
for
className object = new className;
this is because you are stating that a new version of the class is being created and you are assigning it to an object.
So the datatype of an object(if you are creating an object to use a class) will ALWAYS be that of the className.
I really hope this helps, it's a bit confusing because you have to state everything in java where in non object languages the computer will figure it out on its own but like everything practice practice practice! Again I hope this helps!
Aaron Arkie
5,345 PointsHello this was a little difficult for me to understand when i started to so ill give it a go... I'd suggest if you really want to learn the basics of java which finally helped me understand about stuff like this is get Java A beginners Guide 5th ed. by Herbert Schildt. I will be using code from this book to help you.
Java is a object-oriented programming language meaning that in order for a code to compute it must be in an object in order to be used. Lets start from class.
Class is a template of an object, it specifies what the object will have inside of it or what it does. So for example lets say i want to talk about a car. But lets talk about something more broader that will apply to every car and not just a specific car. So we will create a class called Vehicle. In a vehicle we can assume characteristics of how passengers it can hold, how much gas it takes, and how many miles it gets per gallon.
so we have something that looks like this.
class Vehicle {
int passengers; int fuelcap; int mpg;
}
Now we have a class and keep in mind a class is a template of an object; the form of an object depends on the attributes it is given defined in a class.
We can now get into Objects. Remember how i said java is an object-oriented programming language? You can guess that the next step is to use the class we created called Vehicle. So from here we need to put Vehicle into an object.
So we have:
Vehicle sportscar = new Vehicle(); // new just means that you are assigning a new version of that class to an object. Vehicle minivan = new Vehicle(); // so every time we create an object to use the Vehicle class we use new to assign it to that specific object.
Sportscar and minivan are objects used to store the Vehicle class.
now that we stored the Vehicle class in an object called sportscar we can now use the vehicle attributes called passengers, fuelcap, and mpg.
Lastly, we use datatypes to declare what a variable holds. Is it an integer? is it a character?
so for example in the Vehicle class we have passenger , mpg, and fuelcap which all hold a data-type of int which stands for integer.
when we declare a type and a variable and do not assign it a number we are just saying that it holds a number or letter of its type.
example:
type-data variable = ?
so we can say
int passenger; // it holds a value of an integer. or we can say int passenger = 252;
however if you use the first form, thereafter all you need to do is just use the variable name so for example.
Class Vehicle{
int passenger; passenger =12;
}
in conclusion we can say Class is the layout or whats inside the object. Object is the form of what is detailed by a class. Data type is what the variable holds.
I hope this helps! Goodluck it takes awhile to understand and it is difficult but just keep at it.
Also try looking at these tutorials, this is where i officially started to learn programming. Goodluck.
Raju Rajpurohit
544 PointsYou have given a best and very good reply to my question. Its really help me to improve my skill.
I am PHP guy. I have some knowledge about the classes and objects. But in PHP, we don't have to declare datatype to object like
class vehicle{ var passenger = 10, mpg; }
$objVehicle = new vehicle(); echo $objVehicle->passenger;
But in java, We are specifying the data type to an object. This is new for me.
How can I know the data type for an object of a class? Will it same as class name?
Like you have mentioned "Vehicle" in Vehicle sportscar = new Vehicle();
Thanks