FAQ CS1 Week 6

Muddiest Points from Lecture Material

  1. If I'm given a javadoc and a blank piece of paper and told to write a class, how do I go about it?

    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:

  2. What is a constructor in Java?

    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:

  3. What's was it you said about Java supplying a default constructor??

    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{
        public static void main( String args[] ){
            Point p1 = new Point();

    class Point{
        private int posx;
        private int posy;

    Both classes compile as written, because the Java compiler inserts a Point() constructor for us.

  4. How do you decide what's public and what's private when implementing a class?

    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.

  5. When I'm writing a method for a class, when do I need parameters and when don't I, i.e., when should there be something between the parentheses?

    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.

  6. Do class methods need to be physically located before the main method in an application program (class)?

    No. The compiler can locate them whereever they appear.

  7. In lab I had a little trouble figuring out how to write the code for the class, but today on the quiz, I had a great deal of problems. I guess I don't have a complete grasp on it yet.

    There isn't really a question here, but I noticed a couple of common problems that students had on the quiz:

  8. A private variable can be only access by the class itself. Why In other classes, can we still use the variable?

    To accurately answer this question I would need to see an example illustrating what you are asking, but let me say this.

  9. I was a little confussed with passing parameters.

    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.

  10. What is the difference between call-by-value and call-by-reference?

    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.

  11. public int method_name ( argument_list ) returns an int value while public void method_name ( argument_list ) seems only to be able to change variables temporarily. How do you pass a variable by value so that a void method could actually be useful? For example, say you wanted a method to change two variables. You couldn't use non-void method because they only return one variable.

    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.

  12. I thought I understood invoking methods, but I got lost when you traced the code execution where we could see the implementation of the method.

    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 ){
    ....statements to perform the task the method is responsible for

    Then, to use this method in our main class method, we could simply say
    double doubleNumber;
    int intNumber;
    char charVariable;
    method1( doubleNumber, intNumber, charVariable );
    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.

    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!)

  13. In class, we were going through the code where it created the String s1 and then jumped to a method where another s1 was created. The method then called for the information from s1. But how does it know which s1 string to use?

    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.

  14. This week the small thing which I am not able to understand is the difference between referencing Strings and the other objects of any class.

    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!)

Nan C. Schaller
Rochester Institute of Technology
Computer Science Department
102 Lomb Memorial Dr.
Rochester, NY 14623-5608
telephone: +1.585.475.2139
fax: +1.585.475.7100
e-mail: ncs@cs.rit.edu
October 16, 2002