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

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.

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";
}
}

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;
    }
}

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?

commented: but true anyhow +7

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

I see, never mind :)

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";
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 :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.