Home > other >  Why can't I print the object and see what's stored in it?
Why can't I print the object and see what's stored in it?

Time:01-14

I have initialized the constructor in the transaction class and created an object and passed the arguments in it and have initialized the variables , but when i want to print the object it is showing error.

package Test;

public class Transaction {
    
    private int balance;
    private String name;
    private long phno;
    public Transaction(int balance, String name, long phno) {
        
        this.balance = balance;
        this.name = name;
        this.phno = phno;
    }
}
package Test;

public class Main {

    public static void main(String[] args) {
                Transaction ts = new Transaction(1000,"vamsi",1234567);
        System.out.println(ts);
    }
}
Exception in thread "main" java.lang.Error: Unresolved compilation problem: 

    at Test.Main.main(Main.java:5)

CodePudding user response:

The code looks fine, there may be a compile-time problem in your IDE, you can check the Problems view to see detail, or try to clean and rebuild your project.

CodePudding user response:

There are several potential scenarios:

  1. It is not clear if you define those classes within 1 file or separate. If one - that could be a reason for error you get.

  2. As @Bohemian noted, you did not implemented toString() method of an object.

Try to fix those 1 & 2 things at least, then you will get more info to debug.

PS: Without implemented toString() method even if you did not get that compilation error, you would get only the referral name of the object in memory, smth like test.Transaction@6debcae2, and not its state attributes, "what's stored in it" or else.

  •  Tags:  
  • java
  • Related