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 trialhabib kazemi
2,041 Pointswhy there should be mCrystalBall?
why i can't use just
private CrystalBall = new CrystalBall();
why we should write
private CrystalBall mCrystalBall = new CrystalBall();
in video explain it but i did'nt understand completely?
3 Answers
Gunjeet Hattar
14,483 PointsOk let me explain this concept in a little more detail
At first let us consider we have a class called CrystalBall
Now, comes the concept of constructors . By default everytime we create a class a default constructor is initialised. But if we create our won then that it used.
So the question is what is a constructor and how to we create one?
Constructors are used for initialzing data. It is called before the rest of the code is executed. So the main reason we use it is to initialize values right at the beginning.
Constructor always has the same name as of the class and have a structure similar to that of a method but without a return type
class CrystalBall {
CrystalBall() // this is a constructor. Even if you dont create one, it will be automatically created
{
// initialization code
}
}
Now when we have to use the class CrystalBall in some place else, we first create and object of CrystalBall. This is where the line comes in
class someOtherClass {
CrystalBall mCrystalBall = new CrystalBall();
}
More info here Java Tutorial Oracle
There are no Java courses in Treehouse, though refer to the same tutorial link above to get an understanding of the core concepts.
Hope that helps
Stone Preston
42,016 Pointsputting a lowercase m in front of member variables is a standard naming convention. Its basically a thing everyone does so that its easy to recognize a member variable in code.
you could name it whatever you wanted (although CrystalBall probably wouldnt work since its the name of the class), but its best to stick with the naming convention and name it mCrystalBall. you could also name it mSomeCrystalBall or mMyCrystalBall. you can call it (almost) anything, but its best to put a lowercase m in front of whatever you call it
you can read more about naming conventions here: http://en.wikipedia.org/wiki/Naming_conventions_(programming)
Gunjeet Hattar
14,483 PointsLets consider your case
private CrystalBall = new CrystalBall();
So when I look at the left hand side of the statement i.e. private CrystalBall , it is incomplete.
What it is trying to achieve is create a new object of type CrystalBall. Great. But then where is the result of that going to be stored in. You cant store that in*private* and yes neither CrystalBall
Why?
In Java you don't have something like access_specifier data_type = initialization;
Semantically and syntactically it makes no sense.
But now when I say access_specifier data_type variable_name
it makes sense since I know that I am defining a variable of a certain data type that has access provision and to which I store some value or a reference.
I recommend you to take a 2-3 day quick course on Java online, just to grasp the basic understanding. It will help you understand the concepts better in Android.
Hope that helps
habib kazemi
2,041 Pointswhich one is the type of object CrystalBall or new CrystalBall()? which course is better for java?