Originally Posted by
jonathanfox
... wtf "private" and "this" are.
PRIVATE:
Private just means you can only read/write/use what ever it is you claim private from within the class from which it is declared. There are other options, see the link.
THIS:
This always refers back to itself.
For example. You write a class called Apples. You write a class called BagOfApples.
Within the BagOfApples class you have an ArrayList of the Apples which will belong to the BagOfApples class. ie the apples in the bag. The Apple class:
/** File: Apple.java
* @author jps
* This class represents an apple...
*/
public class Apple {
public double diameterInInches;
public boolean hasWorm;
private boolean greatTaste;
/** Constructs an Apple object:
* @param diameterInInches The diameter of the apple measured in inches.
* @param hasWorm True if the apple has a worm in it, false otherwise.
*/
public Apple(double diameterInInches, boolean hasWorm) {
this.diameterInInches = diameterInInches;
this.hasWorm = hasWorm;
this.greatTaste = true;//of course my perfect apples have great taste!
}
//Methods for the public to rate my apples after eating them, and review others feedback on them
/** @return True if the apple has a great taste, false otherwise. */
public boolean hasGreatTaste() {
return greatTaste;
}
/** @param greatTaste Set the great taste of the apple. */
public void setGreatTaste(boolean greatTaste) {
//this.greatTaste = greatTaste;
this.greatTaste = true;//I am a crooked salesman :P
}
}
Notice I used public on diameterInInches and hasWorm. I used public so that anyone anywhere can see the size of my apples, and that they have no worms. Public is a good modifier for things you want anyone anywhere to be able to get their hands on.
Notice I used private on greatTaste. This means this variable can only be used from within the Apple class. The hasGreatTaste and setGreatTaste methods provide a way for anyone anywhere to get and set the variable's value. However, I am a crooked salesman. Hey it happens... My apples are so perfect, even if you try to set greatTaste to false, since the variable is private, and the only other access to it is through my crooked method, I can be sure every apple will always have greatTaste.
So lets look at the first line inside the constructor:
this.diameterInInches = diameterInInches;
Where it says this.diameterInInches will refer to the instance variable of the specific apple object in question at the time. What that means is, assume we are tooling along in the BagOfApples class, filling our bag of appels. As we create each apple, we get a new set of variables that relate to the apple we are creating and putting in the bag. At that point the keyword this just refers to that apple.
Now look at the parameters for the constructor. Notice how they have the same variable names as the variable names used as instance variables of the class. If you just type diameterInInches, the compiler looks within the current scope (at this point the constructor block) and finds the first variable named diameterInInches. The first one found will be the parameter, because it exists within the scope. The instance variable is hidden because once the parameter was found, the compiler assumes that is what you wanted to use and stops looking. By saying this.diameterInInches, the compiler knows you are trying to use the instance variable. Or the diameterInInches of this apple, and not the parameter with the same name.
Originally Posted by
jonathanfox
... some advice on how to write the code for bigger programmes...
Ever heard the phrase "Practice practice practice"? Well in programming its another "p-word"... Pseudocode pseudocode pseudocode...
It is like taking a trip. What do you have to do before you start driving? ...Figure out where to go! You get a map, you figure out your starting point and end point. Then you decide how to get there one road at a time. Do the same thing for your program. Start with a problem to solve. Decide what makes a solution acceptable. Then build your algorithm to get from start to end. Once you have that done you can translate your pseudocode to code. But until you have your pseudocode, trying to write code is like driving without directions.
Ok so if you are going to drive to town, or make a simple apple class, you may not need directions or pseudocode. But if you have never been to town, or wrote a small class, it may be easier to get the hang of using your map/pseudocode on a small project, and not have troubles with that once the project is huge.