Ask your own question, for FREE!
Computer Science 19 Online
OpenStudy (ellecullen):

Homework help asap: public class Giver { public void giveEverythingTo (Looper target) { this.clearSlotsToStack(); target.fillSlots(); } //====================== public static void main (String[ ] args) { Looper steve = new Looper(); Looper don = new Looper(); steve.giveEverythingTo (don); } } using code, what object is the actual parameter? Which object is the formal parameter? Also, what object does the "this"refer to, if this is an alias for "steve" during execution of first call?

OpenStudy (ellecullen):

the answer choices are a. don b. steve c. target d. looper

OpenStudy (bibby):

As I understand it, in this case, 4 would be the actual parameter and i would be the formal parameter ``` int i=4; foo(i); ```

OpenStudy (ellecullen):

okay. I understand from your example. Please correct me if I'm wrong. In my question, the formal parameter is don.

OpenStudy (bibby):

correct. I'm unsure as to the actual parameter

OpenStudy (ellecullen):

For the actual parameter, I think its Looper. What do youthink? I was using the Looper don = new Looper(); statement

OpenStudy (woodrow73):

@bibby correct me if I'm wrong; actual parameter is another word for 'argument' && formal parameter means what is inside the parenthesis of the method header?-- Also, I've been studying the keyword 'this' a little bit - how it can overcome shadowing & call a different constructor from another constructor; but in the context of ElleCullen's program, I'm guessing 'this' refers to the Looper object passed into the giveEverythingTo method-- what would 'this' refer to in a situation like this; (assuming my assumptions above are correct)-- ``` giveEverythingTo(Looper a, Looper b) { this.callLooperMethod(); } ``` thanks in advance

OpenStudy (woodrow73):

just to avoid term confusion -- I mean 'argument' as referring to what you place in the method call's parenthesis.

OpenStudy (bibby):

for reference, it's a void function, right?

OpenStudy (woodrow73):

Ya I suppose -- & the object you pass into the method should be a 'reference object' so as you manipulate it in the method giveEverythingTo(), the same changes should take place to the original object you placed in the method call.

OpenStudy (woodrow73):

``` public static void giveEverythingTo(Looper a, Looper b) { } ``` I have a habit of making methods static in the Main class

OpenStudy (bibby):

I'm more well versed in c++ but this is the various ways I've seen this used. ``` Foo::Foo(int age) { this.age = age; //where age is both the parameter and a data variable } ``` as you mentioned, calling constructors with super/this and also in operator overloading. ``` X& operator+=(const X& rhs) { /* addition of rhs to *this takes place here */ return *this; } ```

OpenStudy (bibby):

in your case, I'm not sure. ``` public static void giveEverythingTo(Looper a, Looper b) { //whatever } ``` gets called using ``` giveEverythingTo(foo,bar); ```

OpenStudy (bibby):

there is no calling object

OpenStudy (woodrow73):

I see, thanks for sharing- I've used 'this' in the same manner you did with your first example too. I'll have to read up on it. and for the case I mentioned above --I meant it to look like this; ``` public class Show { public static void main(String[] args) { StringBuilder sb = new StringBuilder("Hello World"); //reverse the string's order with StringBuilder's reverse method rev(sb); System.out.print(sb.toString()); //prints out sb reversed } public static void rev(StringBuilder s){ this.reverse(); } } ``` though the code doesn't compile, because this doesn't work in a static context- I'll need to do more testing.

OpenStudy (bibby):

I'll write up something quick

OpenStudy (e.mccormick):

The use of Argument is computers comes from math. It is specifically the input to a function: http://www.mathsisfun.com/definitions/argument.html

OpenStudy (woodrow73):

Math functions make a good deal more sense now that I know CS lol.

OpenStudy (lyrae):

The 'this' keyword refers to a instance of the object itself. When creating a new instance this will in that instance be a reference to the instance itself. ``` public class Giver { public void giveEverythingTo (Looper target) { this.clearSlotsToStack(); // <- Call Method in Giver //clearSlotsToStack(); // Same as above target.fillSlots(); // Call method in Looper instance } } ``` Like @bibby wrote it's usefull in geters and seter for cleaner naming (although some people don't like it). That's why @woodrow73 you get a 'not in a static context error' in your example. this is an instance keyword and you simply can't use it in a static context (from a static method). ``` public static void rev(StringBuilder s){ this.reverse(); } ``` is the same as ``` public static void rev(StringBuilder s){ reverse(); } ``` you probably want to do ``` public static void rev(StringBuilder s){ s.reverse(); } ```

OpenStudy (woodrow73):

I see- thanks Lyrae. I got this to compile as I visioned: Main class: ``` public class TestThis { public static void main(String[] args) { numbers n = new numbers(); n.readXY(); } } ``` numbers class: ``` public class numbers { private int x; private int y; public numbers(){ this(100); //use this to call different constructor } public numbers(int a){ x = a/2; //assign 50 to x y = a/2; //assign 50 to y } public void readXY(){ System.out.println(x); //prints out 50 this.readY(false); //calls readY method of numbers class } public void readY(boolean qq){ System.out.println("+"); System.out.print(++y); //prints out 51 (matters which side ++ is on) this.showSum(x+10); //calls showSum, passing int of 60 as argument } public void showSum(int x){ System.out.println(" = " + (this.x + y)); //I use this to reference the field variable x, not the formal param x } } ``` Provides a console output of: ``` 50 + 51 = 101 ```

OpenStudy (woodrow73):

For those reading this- note that the 'this' in the constructor must be in the first line of the constructor.

OpenStudy (woodrow73):

Though I've seen 'this' used in the constructor of things like a MyWindowAdapter class- what is the significance of 'this' in such a context; ``` MyWindowAdapter mwa = new MyWindowAdapter(this); ```

OpenStudy (woodrow73):

I think it somehow relates to event listeners.. if that's the case, where would I substitute 'this' in the following code / constructors like the example above- ``` import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.Random; public class This { private JFrame jf; private JButton[] jb; public static void main(String[] args) { This ttt = new This(); } public This(){ jb = new JButton[3]; jb[0] = new JButton("Wave"); //determine text of 3 buttons jb[1] = new JButton("Exit"); jb[2] = new JButton("Random Food"); jb[0].addActionListener(new Listen()); //add all buttons to same actionListener jb[1].addActionListener(new Listen()); jb[2].addActionListener(new Listen()); jf = new JFrame("Window"); //set title jf.setSize(300, 200); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setLocationRelativeTo(null); //window spawns in middle of screen jf.setLayout(new BorderLayout()); //fancy way to orient buttons on jframe jf.add(jb[0], BorderLayout.WEST); //add button to frame jf.add(jb[1], BorderLayout.CENTER); jf.add(jb[2], BorderLayout.EAST); jf.setVisible(true); //finally display the frame } //add the actionlistener private class Listen implements ActionListener { public void actionPerformed(ActionEvent e) { String actionAsString = e.getActionCommand(); if(actionAsString.equals("Wave")){ JOptionPane.showMessageDialog(null, "Hello World"); } else if(actionAsString.equals("Exit")){ try{ Thread.sleep(400); //delay program .4 s before exiting }catch(Exception ee){ } System.exit(0); //ends program } else if(actionAsString.equals("Random Food")){ Random rand = new Random(); int pick = rand.nextInt(3); switch(pick){ case 0: JOptionPane.showMessageDialog(null, "Eggs bennedict"); break; case 1: JOptionPane.showMessageDialog(null, "Bacon"); break; default: JOptionPane.showMessageDialog(null, "Pizza"); break; } } } } } ```

OpenStudy (woodrow73):

what is a situation when you would call a method using 'this' when you couldn't just call the method directly?

OpenStudy (lyrae):

I can think of a few cases but if you do some googleing you'll probably find some more. #1 Like earlier mentioned in getter and setter methods and to generally increase code readability. #2 To pass and instance of myself to another object. ``` class Foo { Foo (Bar bar) { bar.print(); } } class Bar { Bar() { new Foo(this); } public void print() { System.out.print("Hello from Bar!"); } public static void main(String[] args) { new Bar(); } } ``` Should print ``` Hello from Bar! ``` to StdOut. #3 I had forgotten about this one but a post on Stackoverflow reminded me. "... accessing an outer instance from a nested non-static class: ``` public class Outer { protected int a; public class Inner { protected int a; public int foo(){ return Outer.this.a; } public Outer getOuter(){ return Outer.this; } } } ``` " http://stackoverflow.com/questions/2411270/when-should-i-use-this-in-a-class

OpenStudy (woodrow73):

The example you shared helped me understand- thanks.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!