Results 1 to 5 of 5
  1. #1
    sonny's Avatar
    sonny is offline Senior Member
    Join Date
    Feb 2010
    Location
    North West England
    Posts
    146
    Rep Power
    0

    Question Assign it, or call it again,, (again!)

    I asked this previously but perhaps didn't ask the question properly, and cos i marked it SOVED I'll ask again but differently.

    originally i asked some basic advice on using methods please

    what FON says in his reply makes sense in simple terms (which is how i put the original question.) However, i have progressed a little bit further (not a lot!) with writing methods. and maybe i'm taking his reply too literally.

    what if i want to use getwidth() or have some other expression; in a number of different methods within a class.
    do i still assign it to a variable and pass a copy of it from method to method even though some of the methods may not need that variable.

    i realise that in the case of getWidth(); it is easy to pass by calling it as a parameter of the method call but, that is in fact calling it again. Or I am missing something??

    say i have something like this(below), which is not real code just an example to illustrate what I mean.

    thanks in advance


    Java Code:
    run(){
         first method(getwidth() / 10 );
         last method
    }
    
         first void method (int n){
         .......... does some computation on n
         to find x
         and also finds y
         calls second void method(x,y)
         }
    
         second void method (int x, int y){ 
         ... computation on y to find z;
         ...... doesnt need getWidth();
         someVariable=calls a double method();
         calls object method (someVariable, z)
         }
    
         private double method{
         ......
         computation that needs getWidth();
         return double
         }
    
         private void object method (int sV, int z)
         constructs an object like say a GLabel;
         computation constructing object that needs getWidth();
         }
    
         private void last method();{
         ...........
         another few methods some of which use getWidth(); 
         etc...
         }
    :p I still have my "L" plates on...... directions and explanations are far more help than blaring your Horn! :p Watching:CS106a on YouTube \Reading The Art & Science of Java by Eric S Roberts

  2. #2
    sky
    sky is offline Member
    Join Date
    Nov 2009
    Posts
    96
    Rep Power
    0

    Default

    what if i want to use getwidth() or have some other expression; in a number of different methods within a class.
    do i still assign it to a variable and pass a copy of it from method to method even though some of the methods may not need that variable.
    If you have a lot of methods and they are linked, you can asign it to a variable and pass it through the methods, but if they are not linked you will have to call getWidth() in each method. If you are worried about speed, maybe another way that I think is faster is assigning getWidth to a global variable within the class (that is, a field of the class), and you can use that variable in all the methods (it is a kind of global variable for that class).

    i realise that in the case of getWidth(); it is easy to pass by calling it as a parameter of the method call but, that is in fact calling it again. Or I am missing something??
    Yes, it is in fact calling it again.

    Notice that in the example that you wrote, you are actually losing the value of getWidth in the first method, because you find x and y and you called the second method with those values, but it does not know the original value of getWidth() (n)

  3. #3
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Google "premature optimization" and read up a bit. Why assume that calling getWidth() is less efficient, let alone much less efficient, than looking up a width value from wherever you saved it? Do the correct thing first, and optimize later, when you can see where your program is really spending its time. The correct thing in this case is not "which is better?" but "do I want to make sure my Width value stays the same, or do I want to know what the Width is now?"

    Maybe a better example:
    Java Code:
    public class Rectangle {
        private double len;
        private double wid;
    
        public Rectangle(double length, double width) {
            len = length;
            wid = width;
        }
    
        public void setLength(double length) {
            len = length;
        }
    
        public double getLength() {
            return len;
        }
    
        public void setWidth(double width) {
            wid = width;
        }
    
        public double getWidth() {
            return wid;
        } 
    }
    Now I want to implement getArea(). There are at least three ways I can do it. The first, which I'm sure we would both reject, is to add a new instance variable, and modify setLength() and setWidth() so that they update the area variable as well as the len and wid variables. Then getArea() would look like this:
    Java Code:
        public double getArea() {
            return area;
        }
    The second way is to leave the instance variables alone, and implement getArea() this way:
    Java Code:
        public double getArea() {
            return len * wid;
        }
    The third, which you'd probably be inclined to reject as "too inefficient" is this:
    Java Code:
        public double getArea() {
            return getLength() * getWidth();
        }
    I'd like to persuade you that very, very often -- I would even say more often than not -- the third way is more correct. This is part of embracing the philosophy of object-oriented programming, and the concept of encapsulation. If you have getters and setters in a class, then typically you want to know that the getter is called every time someone wants to get that particular value from your object. In this particular case, it doesn't make a difference, but it's a good habit to get into anyway.

    Sorry this post is so long, but I didn't have time to make it shorter. :)

    -Gary-
    Last edited by gcalvin; 03-17-2010 at 02:58 AM.

  4. #4
    sonny's Avatar
    sonny is offline Senior Member
    Join Date
    Feb 2010
    Location
    North West England
    Posts
    146
    Rep Power
    0

    Default

    thanks sky,
    much appreciated


    Notice that in the example that you wrote, you are actually losing the value of getWidth in the first method, because you find x and y and you called the second method with those values, but it does not know the original value of getWidth() (n)
    thats kind of the point I was trying to illustrate, i didnt pass int n to the second method cos it wasn't needed but, in any event:
    int n != getWidth()
    int n == getWidth /10.

    if i were to apply FON's original advice literally, i would call getWidth(); and assign it to a variable in the first method
    - pass that variable to the second method with x,y
    -- pass it again to the double method, (should have left GetWidth() out of this method to make the point more obvious),
    --- pass it again to object method, which requires it. etc
    which seems like a lot of unnecessary copies of the variable in the stack frame of every method. i was trying to get a handle on the convention for doing this

    I havent come across Global variables yet within my study, but from your explanation of Global variables I kinda get the idea there is a way of doing this without passing a copy of a variable around all over the place.

    many thanks mate:D
    :p I still have my "L" plates on...... directions and explanations are far more help than blaring your Horn! :p Watching:CS106a on YouTube \Reading The Art & Science of Java by Eric S Roberts

  5. #5
    sonny's Avatar
    sonny is offline Senior Member
    Join Date
    Feb 2010
    Location
    North West England
    Posts
    146
    Rep Power
    0

    Default

    thanks Gary,
    I know that IVARS are coming up in the next chapter,
    the only thing i've done so far is is private methods with just the run as public, but i can sort of see logically what is happening in your example and will certainly come back to this example.

    hmmm "premature optimization" sounds like something my wife complains about ;-)
    ill be sure to have a look at that too.
    i'm still doing methods at the moment, and doing them much better since your previous post :D I'm not a jump head first kind of learner, especially when teaching myself, I kinda like to go feet first and get lots practice till i'm confident i understand and dont make stupid mistakes the drawback to learning this way is that trying to make the exercises more challenging to get more practice i'm sometimes trying to solve a problem that i cant solve with what i have learned so far. but i like that way of learning

    Cheers
    Sonny
    :p I still have my "L" plates on...... directions and explanations are far more help than blaring your Horn! :p Watching:CS106a on YouTube \Reading The Art & Science of Java by Eric S Roberts

Similar Threads

  1. assign a value to two dimensional array
    By makpandian in forum New To Java
    Replies: 4
    Last Post: 10-05-2011, 06:21 AM
  2. Replies: 2
    Last Post: 02-18-2010, 06:26 PM
  3. Array Assign Values from a Textfile
    By fawadafr in forum Java Applets
    Replies: 6
    Last Post: 11-30-2008, 01:10 AM
  4. Help! Need to randomly assign boxes
    By newb101 in forum New To Java
    Replies: 1
    Last Post: 09-16-2008, 11:57 PM
  5. Assign a keyboard key to a JButton.
    By gszauer in forum AWT / Swing
    Replies: 1
    Last Post: 12-15-2007, 11:42 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •