0

Hi, I'm trying to understand a java concept, I'm trying to get a piece of code to work without changing the main method.

public class TestProg2{
  public static void main(String args[]){
    String filler1="Rank";
    String filler2="Suit";
  Card card = new Card();
  System.out.println(card);
  }  
}

class Card{
  public String Card(){ 
    String test="of"; 
    return test;
  }
}

I keep getting the data location: Card@5a5622

5
Contributors
8
Replies
9
Views
5 Years
Discussion Span
Last Post by DavidKroukamp
Featured Replies
  • 1

    if you print an object using the print or println method, it will automatically call the toString method of that class. if you don't provide one yourself, it will call the toString method of the Object class, which returns a String representation as you described it. if you want it … Read More

  • [CODE] public class TestProg2{ public static void main(String args[]){ String filler1="Rank"; String filler2="Suit"; Card card = new Card(); System.out.println(card); } } class Card{ public String Card(){ String test="of"; return test; } public String toString () { return "This is the toString() method in the Card Class"; } }[/CODE] Read More

  • [QUOTE=Goldfinch;1748951]Hi, I'm trying to understand a java concept, I'm trying to get a piece of code to work without changing the main method. [CODE=java] public class TestProg2{ public static void main(String args[]){ String filler1="Rank"; String filler2="Suit"; Card card = new Card(); System.out.println(card); } } class Card{ public String Card(){ String … Read More

  • No, I dont agree. First of all, you are creating a constructor which returns a String. That is not possible, all constructors are voids. Next, toString() methods should just return a straight string, why would you call a Constructor? Read More

  • [CODE]static String test=null; public Card() { setValue();//if we dont call this our value wont be set } public static void setValue() {//set value test="of"; }[/CODE] Why such ridiculous contortions? Why not just [CODE]static String test="of";[/CODE] Read More

1

if you print an object using the print or println method, it will automatically call the toString method of that class.

if you don't provide one yourself, it will call the toString method of the Object class, which returns a String representation as you described it.

if you want it to print a (for you) meaningful text, you'll have to override the toString method.

1
public class TestProg2{
  public static void main(String args[]){
    String filler1="Rank";
    String filler2="Suit";
  Card card = new Card();
  System.out.println(card);
  }  
}

class Card{
  public String Card(){ 
    String test="of"; 
    return test;
  }
public String toString () {
   return "This is the toString() method in the Card Class";
}
}
1

Hi, I'm trying to understand a java concept, I'm trying to get a piece of code to work without changing the main method.

public class TestProg2{
  public static void main(String args[]){
    String filler1="Rank";
    String filler2="Suit";
  Card card = new Card();
  System.out.println(card);
  }  
}

class Card{
  public String Card(){ 
    String test="of"; 
    return test;
  }
}

I keep getting the data location: Card@5a5622

I agree with hiddepolen's method however i would just add a constructor to the Card Class to call the method Card() within the Card class similar to this:

class Card {
static String test=null;
    public Card() {//constructor that will be called from main by initiating instance of class
        setValue();//if we dont call this our value wont be set
    }
 
    public static void setValue() {//set value
        test="of";
    }
 
    @Override
    public String toString() {
        return test;
    }
}

Edited by DavidKroukamp: n/a

1

No, I dont agree.
First of all, you are creating a constructor which returns a String. That is not possible, all constructors are voids.
Next, toString() methods should just return a straight string, why would you call a Constructor?

Comments
but true anyhow
0

No, I dont agree.
First of all, you are creating a constructor which returns a String. That is not possible, all constructors are voids.
Next, toString() methods should just return a straight string, why would you call a Constructor?

lol i just done editing when you posted this see up :D

Edited by DavidKroukamp: n/a

1
static String test=null;
public Card() {
 setValue();//if we dont call this our value wont be set
}  
public static void setValue() {//set value
 test="of";
}

Why such ridiculous contortions? Why not just

static String test="of";
0
static String test=null;
public Card() {
 setValue();//if we dont call this our value wont be set
}  
public static void setValue() {//set value
 test="of";
}

Why such ridiculous contortions? Why not just

static String test="of";

lol yes was working and adjusting OP's code, never saw that but thanks :)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.