The first thing is don't panic! The javadoc gives you lots of information. Here are the things I would do and the order in which I would do them:
public class classname {and at the bottom of the page the closing }
public classname(...parameter list....) {Leave some space and put the closing }
public returntype methodName(...parameter list....) {
for instance methods and for class methods
public static returntype methodName(...parameter list....) {Leave some space and put the closing }
IF these variables and constants do NOT show up in the javadoc, they must be private! If they do, they must be public.
Be sure to use static for the class variables and constants. These are the ones that there should only be one copy of, no matter how many objects have been created. Be sure NOT to use it when you want to have a different value of that variable/constant for each object!
Don't forget to specify the type of value that the identifier is to contain! Again the javadoc will provide lots of hints!
If it's a constant, don't forget to use the word final!
If it's a class variable that is a counting number of items created, it should be initialized to 0.
this(...with the appropriate arguments...)Generally, a constructor will initialize all instance variables and perhaps manipulate a class variable. The javadoc will tell you what to do! A constructor will NOT have a new or the name of the class in it!!!! Nor, will it allocate space explicitly for anything.
If you are working on a method that has a return value and what gets returned is NOT an instance variable/class variable itself, then it is likely to be something that may be calculated based on these variables.
A constructor is a special method in Java that is called when an object is instantiated, i.e., when new is used in an application or applet. Some characteristics of a constructor are:
If you do not provide ANY constructor, Java will insert a default constructor for you. A default constructor is one that has NO formal parameters. For example:
public class Difficult{Both classes compile as written, because the Java compiler inserts a Point() constructor for us.
public static void main( String args[] ){
Point p1 = new Point();
}
}
class Point{
private int posx;
private int posy;
}
Well, it depends on what your starting point is. If you are writing a class using documentation produced by javadoc, then every constant, variable, and method that is visible on the html page IS public. Any instance or class variables and instance or class methods that aren't shown are private.
If you are creating a class from scratch, then making those decisions is part of the design process. In general, if you want the "public" to be able to use items directly, you'll make them public, if not you'll make them private. For example, if you are creating a class to manage a bank account, you probably want the "balance" to be private and only alterable by using "addMoney" and "withdrawMoney" methods, which would have to be public.
What you need to think about is whether the object's instance variables contain all of the information needed to perform the task that the method is supposed to perform. If so, no parameters are needed. If not, then parameters must be defined that contain the information that is needed.
For example, in lab, when you are asked to compare two Strings, the method to do the comparison is invoked on one of the String objects. To do the comparison, you need another String object. In this case, then, the parameter would be the other String object. On the other hand, when implementing the Circle class from the shape classes used in Lab 3, all of our methods (getArea, getPerimeter, getRadius) had all of the information they needed to do the work in the instance variable radius, so no parameters were needed.
No. The compiler can locate them whereever they appear.
There isn't really a question here, but I noticed a couple of common problems that students had on the quiz:
The application program is the one that will have a main method in it. "TestSquare", in our case, will create and instantiate objects of the "Square" class, for example, use new Square( 5 ).
The class you are writing to be USED by an application will actually implement the constructors and methods of that class, in our case, "Square".
To accurately answer this question I would need to see an example illustrating what you are asking, but let me say this.
You say you are a bit confused over parameter passing, but you don't ask a specific question. You may want to check these pages for previous weeks. I will say a little below.
Java does all of its parameter passing by value. That means the method that is invoked gets a COPY of what's in the actual parameter (argument) or what the expression used for the argument evaluates to. If the argument is a primitive type, i.e., a value, the formal parameter gets a COPY of the VALUE of the primitive. If the argument is an object, i.e., the argument references an object, it gets a COPY of the REFERENCE to the object. So, what it's REALLY essential to understand clearly is the way primitive and objects differ, and how each works, i.e., the material from weeks 2 and 3.
Java passes all parameters using call-by-value. This means that the formal parameter gets a copy of the contents of the actual parameter. The effect of this though changes depending on whether the parameter is a primitive type or a reference to an object. In the case of a primitive, this means that no matter how much the formal parameter gets changed while the method is executing, the actual parameter will NOT be changed. However, in the case of passing a copy of a reference to an object, if the method changes the contents of the object that is referred to, then the invoking method will see that change (i.e., there is a side-effect, which may be expected or may not be).
Some languages use call-by-reference to pass parameters. In this case, the actual and formal parameters name the SAME memory location, and, therefore, any changes made within the method to parameter identifiers WILL be seen in the invoking method.
First of all, a void method MIGHT actually change the contents of an object available to the invoking method. (See the questions below.)
Although your general question is an interesting one, I'm not really going to answer it directly. This is because doing this kind of thing is not considered good programming style. Rather, it is better to limit what a method can do so that it performs a small, concise task. This adds to code readability. If you truly felt that both variables are related enough to be altered by a single method, then it is likely that these variables should be instance variables of the same object rather than separate (isolated) variables. Then you could return the object type from the method, or pass the object to a "void" method which could alter the contents of the object (i.e., in the latter case, creating side-effects that may not be obvious to the reader). BUT, even so, it is often better to have two separate modifier methods, one for modifying each of the individual instance variables rather than one single method to do both.
Don't panic! I'm sure this will clear up with time. In Java, the convention is that our applications start executing in the main class method. We will start with the first executable line in main, followed by the next and the next, until we get to the } that terminates the main class method. The only variation from this occurs when we execute selection or repetition statments which will be covered week 4 of the quarter OR when a method is invoked.
When a method is invoked, we start executing the statements within the invoked method. When all of the statements of the method have been executed, we return to the invoking method and begin executing at the statement below the one where the method was invoked, assuming the invoked method returned a void. If the method returns a value than that returned value is USED in the statement where the method was invoked, and execution continues from there.
The method that is invoked may need info to do its task from the invoking method. We can often find out what info it needs by looking at the javadoc for the classes we are using. But, we may also create methods (in our case, they were class helper methods) to help us complete our task. For example, we might define a method
public static void method1( double a, int b, char c ){Then, to use this method in our main class method, we could simply say
....statements to perform the task the method is responsible for
}
double doubleNumber;Notice that the number, types and orders of the actual parameters (doubleNumber, intNumber, and charVariable) match those of the formal parameters ( double a, int b, char c ) in the method definition.
int intNumber;
char charVariable;
...
method1( doubleNumber, intNumber, charVariable );
When the method is invoked, copies of the values of the primitives (or references in cases when the parameter is an object) are copied into the primitive type variables (parameters) a, b, and c that come into being upon method invocation. These variables (the formal parameters) will only live while the method is executing. (NOTE: This may include time when the method itself invokes other methods!)
The concept of what's known where is called "scope". The best rule of thumb with respect to what we've learned so far is that within any block of code (code between {}'s), the closest declared identifier of that name is used. So in our case, in the main class method, we use the s1 that's declared there. If there was no s1 declared there, we'd look in the next outside block (the class block in this case) and so on.
In the methods, we use the s1 in the parameter list. (The parameter list is the receiver of what the method needs to do its work.) If you look at the parameter list, you'll notice that there is kind of a declaration there - the type followed by the identifier. You may also declare other (local) variables within a method.
Sorry - your question is muddy to me! :-) I really don't understand what you are asking. If your "question" refers to the difference between the String and StringBuffer classes and when the contents an object of these types might have be changed in the invoking method upon returning from another method, then a contributing factor in the answer is that a String object in Java is immutable and its contents cannot be changed. So, if a method (apparantly) changes the contents of the String object that its formal parameter refers to, the parameter itself always gets a NEW reference to a NEW object within the method. (Remember parameters only have memory allocated to it as long as the method is executing.) Thus, when the method is finished executing, the variable (actual parameter) in the invoking method still refers to the original String and is unchanged.
However, in the case of our StringBuffer example, the method StringBuffere method reverse() changed the contents of the StringBuffer object itself (i.e., the reference, in the parameter itself, doesn't get changed). This means that we see the "side-effect" of this change in the invoking program. The CONTENTS of the object that the actual parameter referred to reflects the change.
Take a look at ObjectPassing.java and its output, for an example of this behavior. To see what happens if the parameters are primitives, see PrimitivePassing.java and its output. (If you'd like to download and run them, hold the shift key down while you left click!)