-1

When creating a method inside a class if you use the parameter:

public String Method(ClassName NewObject){}

or in my example below:

public String EqualsTo(Deck aCard){}

will it create a new object of that class within that method? If not anyone mind explaing what I is happening with that parameter?

NOTE: Disregard any minor syntax errors as I just constructed this to get my question across better so this is not a complete class.

import java.util.Scanner;
import java.util.Random;

public class Deck {
private int suit;
private int rank;
private Random generator = new Random();
Scanner in = new Scanner(System.in);

    //Default constructor
    public Deck() {
    suit = suit.random(4);
    rank = rank.random(13);
    }


public String EqualsTo(Deck aCard){}
}
Bob
  • 746
  • 3
  • 11
  • 26

4 Answers4

1

Objects are created when you use the new keyword. On the other hand, when you declare a method as

public String EqualsTo(Deck aCard){}

it says that the EqualsTo() method takes a Deck reference as a parameter. In order to use the method, you need to create a Deck object and keep a reference to it. Then you send the reference to the method.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • Can you create a new Deck in Main.Java and pass that as a parameter with the equals to for example (Assume I created the object already as NewDeck): `Deck.EqualsTo(NewDeck);` – Bob Feb 12 '13 at 03:52
  • @BobDunakey Yes. That is (almost) exactly how `EqualsTo()` will work. However, you must use an instance of a deck, both to call the method and as a parameter. You cannot use the name of the `Deck` class itself to call this method. – Code-Apprentice Feb 15 '13 at 00:34
1

That parameter act as a place holder for that type of object. It doesn't actually create it, the method signature says:

  1. I'm an EqualsTo() method and I take/require a Deck type of instance/object as a parameter.

  2. It return a String as a result.

By the way, I recommend using a boolean as the return type instead of a 'String' for an EqualsTo method.

TheLazyChap
  • 1,852
  • 1
  • 19
  • 32
1

Fundamental thing: Java is pass by value.

  1. A new object is not created.
  2. Object reference value created by the calling entity is copied to the method argument aCard.
  3. Any modifications done on this object will remain visible to the calling entity. This is because the reference value is pointing to the same address location of the object.

Here's an example.

public class Test2 {

    public int bla;

    public static void main(String[] args) {
        Test2 test = new Test2();
        test.bla = 878;
        test.doSome(test); // Calling Location
        System.out.println(test.bla);
    }

    public void doSome(Test2 test) {
        test.bla = 95;
    }
}
Makoto
  • 104,088
  • 27
  • 192
  • 230
user1428716
  • 2,078
  • 2
  • 18
  • 37
1

NO

public String EqualsTo(Deck aCard){}

Does not create a new object when it is called.

Here is where the objects are created. Use case

Deck deck1 = new Deck(); // NEW OBJECT CREATED
Deck deck2 = new Deck(); // NEW OBJECT CREATED

String result = deck1.EqualsTo(deck2); // NO NEW OBJECT CREATED, 
                                     // JUST PASSED REFERENCE OF EXISTING OBJECT
                                     // (except the result of course, which is probably a new object)
user000001
  • 32,226
  • 12
  • 81
  • 108