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

Android

Android Java Classes and Objects

May someone kindly explain more in brief with an example, im confused about Classes and Objects

1 Answer

In Java, which is the language for Android, a class is a reference for the creation of an object, also called instantiation. When we create the class in the code, it then generates a file which can be used actively in our program for things such as methods, attributes and other activities within our program.

Classes are basically guides for building objects. A class starts with a statement that defines its accessibility, its return value and its parameters. So it would look something like this:

public String userName(String, name){
   name = mUserName;
}

This is an example of a class that would be used to assign the member variable, mUserName to the parameter name when the class is instantiated into an object.

So when we create a new object, the code might look something like:

new userName("Mark");

This code would define a new username object into the system with the string Mark as the parameter, saving it into the member variable mUserName so it can be securely accessed later.

This is confusing at first, but keep coding and it will make more and more sense. I also suggest you take the Java course before you do Android as adding Android into zero Java knowledge will make it harder.

Thanks,