Hi All,

I have created a Class named Stock and have declared three Constructor . First constructor with no parameter, second constructor with three parameter, third with Two parameter.

When I call my constructor inside a System.out.println(constructor); , it should display the variable Values right ? . but instead it outputs default toString Value.

Please explain this behaviour.

output :


ouput im expecting is (as per the book im referring)

20 sa 4.0

my code is as follows :

// this is the Stock class which has constructor

public class Stock {

    private int noOfShares;
    private String tickerSymbol;
    private double dividend;

    public Stock() {
    public Stock(int inNoOfShares,String inTickerSymbol,double inDividend)
        noOfShares = 10;
        dividend = 23;
    public Stock(int inNoOfShares,double inDividend)
        dividend = 46;


import stock.Stock;

public class StockTest {

  public static void main(String[] args) {    
        int noOfShares;
        String tickerSymbol;
        double dividend;   

        Stock stockOne ;
        Stock stockTwo ;
        Stock stockThree ;

        stockOne = new Stock(20,"sa",4.0);


Recommended Answers

All 7 Replies

The only way to make println not call the default toString method is to define your own toString method. If you want the contents of the variables to be printed, you'll need to define a toString method that returns a string containing the contents of those variables.

Actually,stockOne is ur object right?

If u do like this the memory location of your object wil be printed as output.

Try using toString() method. I guess it'll solve your problem.

Yes, as sepp2k suggested, you will need to create your own toString() method, in order to output the information in the format that you will like. However, another problem I can see arising, is the fact that no matter what values you create the objects with you seem to define a default value for them; for example your object you declared here:
stockOne = new Stock(20,"sa",4.0);
Will ultimately be reset to
noOfShares: 10; tickerSymbol: firstTicker; dividend = 23
You need to ensure that when you create the object that the values you are passing to the constructor are being assigned those values, this may be a helpful resource for you. If you have any questions, I have your code working the correct way (IMO), and am more then willing to help.

Thanks Sepp2k, Vani and Patrick four your explanation :)

I understand now what went wrong in my code ...

Now i only have one doubt more like a clarification. What is the advantage of using "this" operator inside constructors while assigning values to variables. Using "this" operator and using without "this" operator produces the same result.

here is my code

public Constructor(int x, int y)

        this.a = x; //  a = x; also produces same output
        this.b = y; //  b = y; also produces same output

    public Constructor(int x)


In that case,, yes. But to make things clearer people often use the same names for the variables and the parameters

class X {
   String name;
   public X(String name) {
      this.name = name;  // now I need "this" to refer to the variable not the parameter

"this" will refer to the reference of current object.. not the parameter or any other stuff.

Got It .... Thanks James and Vani !

Case Closed :)

Be a part of the DaniWeb community

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