-4

I'm working with Java. I have a class with 2 constructors. The first constructor takes an int value as a parameter and sets an int variable as that value. The second constructor takes a string and prints it out. The idea is that when I call the first constructor from my main class, it sets an integer value. And when I call the second constructor in the main class, it takes the string representation of int variable of the first constructor and prints it out.

Here's how I made the constructors:

public class Test
{
   int val;

   public Test(int x)
   {
      val = x;
      return val; //I know this won't work. So I'm looking for an alternative
   }

   public Test(String y)
   {
      System.out.println("The value is " + y);
   }
}

And the main method (in a different class) looks like this:

public static void main(String [] args)
    {
        Test t1 = new Test(6);
        Test t2 = new Test(String.valueOf(t1)); //This won't work because the first constructor can't return a value
    }

So how exactly can I change the contents of the constructors so that I can pass val into the 2nd constructor?

Vktr
  • 89
  • 8

5 Answers5

2

Override toString() to return value so when you so String.valueOf(t1) it will do the toString() method;

public class Test
{
   int val;

   public Test(int x)
   {
      val = x;  
   }

   public Test(String y)
   {
      System.out.println("The value is " + y);
   }

   @Override
   public String toString()
   {
      return String.valueOf(val);
   }
}
locus2k
  • 2,802
  • 1
  • 14
  • 21
1

I think what you are probably actually trying to do is to override the toString() method of Test.

public class Test
{
   int val;

   public Test(int x)
   {
      val = x;
   }

   @Override
   public String toString() {
       return "Test:"+val;
   }
}

Then you can do this:

public static void main(String [] args)
    {
        Test t1 = new Test(6);
        String s = t1.toString();
        // or this
        System.out.println( t1 ); // prints "Test: 6"
    }
markspace
  • 10,621
  • 3
  • 25
  • 39
0

What you're describing is actually impossible without some changes.

First and foremost, t1 and t2 are two separate instances and the values inside of them have no bearing on one another. So t1 has x=6 and t2 has x=0 (because of default values).

If you want your second constructor to have a value of x that isn't 0, then you'll need to pass that in too.

public Test(int x, String s) {
    super(x);
    System.out.println(x);
}
Makoto
  • 104,088
  • 27
  • 192
  • 230
0

I think you don't really want two constructors. It seems like you're wanting to do something like the following:

public class Test
{
   int val;

   public Test(int x)
   {
      val = x;
   }

   public void printVal()
   {
      System.out.println("The value is " + val);
   }
   public static void main(String [] args)
   {
      Test t1 = new Test(6);
      t1.printVal();
   }
}
Ryan Cogswell
  • 75,046
  • 9
  • 218
  • 198
  • I would not use `printVal()`, but instead override the `toString()` method. In that case, you can do `System.out.println(t1.toString())` to print the value (including the message) to the screen. – MWB Nov 13 '18 at 17:32
  • Yes, I agree. Answer by @markspace is a better end result. I was trying to stay closer to the original code. – Ryan Cogswell Nov 13 '18 at 17:33
0

Your requirement is kinda weird. But this will work even it is kinda weird

public class Test {
    private static int val;

    public Test(int x) {
        val = x;
    }

    public Test() {
        System.out.println("The value is " + String.valueOf(val));
    }

    public static void main(String[] args) {
        Test t1 = new Test(6);
        Test t2 = new Test();
    }
}
Sandeepa
  • 3,457
  • 5
  • 25
  • 41